Classes and objects in Java
In Java, nearly everything is organised into classes. Understanding fields, constructors, methods, and access modifiers is the core of Java programming.
Fields and methods
A class defines fields (data) and methods (behaviour). public class Circle { private double radius; public double area() { return Math.PI * radius * radius; } }. Each object, created with new, has its own copy of the instance fields: Circle c = new Circle();.
Constructors
A constructor initialises a new object. It has the same name as the class and no return type: public Circle(double radius) { this.radius = radius; }. this refers to the current object, distinguishing the field from the parameter. Classes can have several constructors with different parameters, an example of overloading.
Access modifiers
Access modifiers control visibility. private members are visible only inside the class; public members are visible everywhere; protected members are visible to subclasses and the same package; with no modifier, visibility is limited to the package. Making fields private and exposing methods is encapsulation.
Getters and setters
Private fields are often accessed through methods: public double getRadius() { return radius; } and public void setRadius(double r) { if (r < 0) throw new IllegalArgumentException(); radius = r; }. Setters can validate values, keeping objects in a valid state. Records, added in Java 16, create simple immutable data classes concisely: record Point(int x, int y) {}.
Static members
static fields and methods belong to the class rather than to any object. Math.sqrt is static: you call it on the class, with no Math object needed. A static counter field can track how many objects have been created. main is static because it runs before any objects exist.
Method overloading
Several methods can share a name if their parameter lists differ: int add(int a, int b) and double add(double a, double b). The compiler chooses the right one from the argument types. Overloading differs from overriding, which replaces an inherited method in a subclass.
Objects and references
Variables of class type hold references to objects, not the objects themselves. After Circle a = new Circle(1); Circle b = a;, both names refer to the same circle, so changing it through b is visible through a. A reference that points to no object is null; calling a method on it throws NullPointerException, one of Java's most common errors.
Worked example
public class Counter { private static int created = 0; private int value; public Counter() { created++; } public void increment() { value++; } public int getValue() { return value; } public static int getCreated() { return created; } }. After making two counters and calling increment three times on the first, the first's value is 3, the second's is 0, and Counter.getCreated() is 2.
Key takeaways
Classes define fields and methods; new creates objects with their own instance fields. Constructors share the class name, have no return type, and use this. private, public, protected, and package access control visibility; private fields with public methods give encapsulation. Getters and setters can validate; records make concise immutable data classes. static members belong to the class. Overloading reuses names with different parameters. Variables hold references; null references cause NullPointerException.
Flashcard
Data stored in an object
Check your understanding
A field is declared private double balance;. Which code can read it directly?
