SwadhyAI Notes

All Java notes

Java Class Attributes

›Short versionOpen if you only have a minute.
  • An attribute is a field declared in the class. Instance fields are stored once per object.
  • A static field is stored once on the class. Every object sees the same value.
  • Untouched fields default to 0, false, or null. Local variables do not get those defaults.
  • A final field is assigned once, by the time the constructor finishes. A final reference can still point at a mutable object.
  • Put lasting state in a field. Compute a value that is fully determined by other fields when it is needed.
  • Instance initializers run after the superclass constructor returns, and before this constructor body. Static initializers run once, when the class is initialized.

Two cars leave the same factory drawing. One is blue, one is green. The drawing says a car has a color. The paint is on the car, not on the drawing. In Java the drawing is the class, and the paint is an .

What belongs on the object

A is state the object still needs after the method that set it has returned. Color, plate, and odometer are fields. A temporary sum inside a method is a local variable. If nothing reads the value later, it should not become a field.

Car.java
public class Car {
    private final String plate;
    private String color;
    private int odometer;

    public Car(String plate, String color) {
        this.plate = plate;
        this.color = color;
    }

    public void drive(int kilometers) {
        if (kilometers < 0) {
            throw new IllegalArgumentException("kilometers");
        }
        odometer += kilometers;
    }

    public int odometer() {
        return odometer;
    }
}

odometer has a of 0, which is the right start, so the constructor leaves it alone. plate is . Each constructor path assigns it once. color can change later, so it stays a normal instance field.

The field = 0 would be redundant here because 0 is already the default. An initializer earns its place when the starting value is something else, such as private int seats = 4. That assignment runs for every new car, after the superclass constructor returns and before this constructor body.

One counter for the whole class

A is the factory log, not the paint. Every car can read it. There is still only one log.

java
public class Car {
    private static int nextSerial = 1;
    private final int serial;

    public Car() {
        serial = nextSerial;
        nextSerial++;
    }
}

The first new Car() takes serial 1. The second takes serial 2. serial is per object. nextSerial is not. Static fields are also visible to static methods, which have no particular car to read an instance field from.

Reading the fields

Outside the class, private fields are invisible. Callers use methods. Inside an instance method, the bare name odometer means the field of the current object. A parameter of the same name hides it, which is why constructors write this.plate = plate.

Subclasses do not get a second copy of a field when they declare the same name. They hide the superclass field. Methods override. Fields do not. Read the field through a method if the subclass should be able to change what "the value" means.

Car a = new Car(); Car b = new Car(); a.drive(10); What is b.odometer() if odometer is an instance field, and what changes if nextSerial is static?

b.odometer() is 0. The drive call updated the field inside a only. nextSerial moved for both constructions because there is one static counter. Building b consumed the next number after a.