Understanding the Class Structure

Understanding the Class Structure

OCP Exam Topic — Understand the structure of a Java class: fields, methods, constructors, and how they relate to objects. Covered in Chapter 1 of OCP Java SE 21 Developer Study Guide (Selikoff & Boyarsky).


The Anatomy of a Java Class

Everything in Java lives inside a class. A class is a blueprint that defines the state and behaviour of objects.

java
public class Animal { // Fields — state String name; int age; // Constructor — object creation public Animal(String name, int age) { this.name = name; this.age = age; } // Method — behaviour public String describe() { return name + " is " + age + " year(s) old."; } }

Fields

A field (also called an instance variable or member variable) stores the state of an object:

java
public class Circle { double radius; // no access modifier → package-private String color; }

Fields are declared directly inside the class body, outside any method. They receive a default value when the object is created:

Type Default Value
boolean false
byte, short, int, long 0
float, double 0.0
char ''
Object reference null

Methods

A method defines the behaviour of an object. Its structure is:

plaintext
accessModifier returnType methodName(parameterList) { // body }
java
public class Calculator { // No parameters, returns an int public int getDefault() { return 0; } // Two parameters, returns a double public double add(double a, double b) { return a + b; } // void: returns nothing public void printResult(double result) { System.out.println("Result: " + result); } }

Method Signature

The method signature is the combination of the method name and its parameter list (types and order). The return type is not part of the signature.

java
// These have DIFFERENT signatures — different parameter types public void process(int x) { } public void process(String s) { } // SAME signature — compile error if both are in the same class // public void process(int y) { }

Constructors

A constructor initialises a new object. It has the same name as the class and no return type:

java
public class Book { String title; int pages; // Constructor public Book(String title, int pages) { this.title = title; this.pages = pages; } }

If you do not declare any constructor, the compiler provides a default no-argument constructor automatically. Once you declare any constructor, the compiler no longer provides the default.


Comments

Java supports three styles of comment:

java
// Single-line comment /* Multi-line comment */ /** * Javadoc comment — used to generate API documentation. * @param name the animal's name */ public void setName(String name) { ... }

File and Class Naming Rules

The exam tests these rules carefully:

Rule Detail
A file may contain one public class The filename must match the public class name exactly
A file may contain multiple non-public classes Only one can be public
Filename is case-sensitive Animal.java ≠ animal.java on Unix/macOS
Each class compiles to its own .class file Even classes in the same .java file
java
// File: Animal.java public class Animal { } // OK — filename matches class Helper { } // OK — non-public companion class // public class Other { } // ERROR — second public class in same file

Top-Level vs Nested Classes

All the classes in this module are top-level classes (declared directly in a file, not inside another class). The exam also covers nested and inner classes in a later module.


Order of Elements Inside a Class

The Java specification does not mandate the order, but the conventional and exam-expected order is:

  1. Fields
  2. Constructors
  3. Methods
java
public class Dog { // 1. Fields String breed; // 2. Constructor public Dog(String breed) { this.breed = breed; } // 3. Methods public void bark() { System.out.println("Woof!"); } }

Key Points to Remember

  • A class contains fields (state) and methods (behaviour); constructors initialise objects.
  • A field's default value depends on its type; local variables have no default and must be assigned before use.
  • A public class name must match its filename exactly.
  • The method signature is name + parameter list; return type is not part of it.
  • If no constructor is declared, the compiler inserts a default no-arg constructor.
  • A single .java file may contain multiple classes, but only one may be public.