OBJECT-ORIENTED PROGRAMMING

Java class attributes

Class attributes store an object's data and describe its state. In Java, they are also called fields ( fields) or instance variables.

What is a class attribute?

An attribute is declared inside a class but outside its methods. Each object created from the class gets its own instance attribute values.

public class Knyga {
    String pavadinimas = "Mažasis princas";
    int puslapiuSkaicius = 96;
    boolean perskaityta = false;
}
There is no console output.

Only the class attributes are defined here. No object is created and there is no println(), so nothing is printed.

  1. pavadinimas is String attribute, so it stores text.
  2. puslapiuSkaicius is int attribute, so it stores an integer.
  3. perskaityta is boolean attribute, so its value can only be true or false.
  4. All attributes are written inside the class but outside methods.
  5. Every future Knyga object will get its own values for these attributes.

Accessing attributes

An object's attribute is accessed with the dot operator: objektas.atributas.

public class Knyga {
    String pavadinimas = "Mažasis princas";
    int puslapiuSkaicius = 96;

    public static void main(String[] args) {
        Knyga knyga = new Knyga();

        System.out.println(knyga.pavadinimas);
        System.out.println(knyga.puslapiuSkaicius);
    }
}
Console output

Mažasis princas
96

  1. With new Knyga() creates a specific book object.
  2. The variable knyga lets you access this particular object.
  3. knyga.pavadinimas uses a dot to access the object's text field.
  4. knyga.puslapiuSkaicius accesses its numeric field.
  5. Both values appear on separate console lines.

Changing attribute values

If an attribute is not final, it can be assigned a new value of a compatible type:

public class Knyga {
    String pavadinimas = "Mažasis princas";
    boolean perskaityta = false;

    public static void main(String[] args) {
        Knyga knyga = new Knyga();

        knyga.pavadinimas = "Pepė Ilgakojinė";
        knyga.perskaityta = true;

        System.out.println(knyga.pavadinimas);
        System.out.println(knyga.perskaityta);
    }
}
Console output

Pepė Ilgakojinė
true

  1. The new object initially has the title Mažasis princas and the value false.
  2. knyga.pavadinimas = "Pepė Ilgakojinė" replaces the old text with new text.
  3. knyga.perskaityta = true marks the book as read.
  4. println() displays the updated values rather than the initial ones.
  5. The changes belong to this particular knyga object.

Each object has its own state

Objects created from the same class can store different attribute values:

public class Knyga {
    String pavadinimas;
    int puslapiuSkaicius;

    public static void main(String[] args) {
        Knyga pirma = new Knyga();
        Knyga antra = new Knyga();

        pirma.pavadinimas = "Mažasis princas";
        pirma.puslapiuSkaicius = 96;

        antra.pavadinimas = "Pepė Ilgakojinė";
        antra.puslapiuSkaicius = 160;

        System.out.println(pirma.pavadinimas + ": " + pirma.puslapiuSkaicius);
        System.out.println(antra.pavadinimas + ": " + antra.puslapiuSkaicius);
    }
}
Console output

Mažasis princas: 96
Pepė Ilgakojinė: 160

  1. The same Knyga class creates two separate objects.
  2. The first object is assigned one title and 96 pages.
  3. The second object is assigned another title and 160 pages.
  4. Each object has its own separate field values.
  5. The console therefore shows two different books and their page counts.
pirma and antra are independent objects.

Changing one book's title or page count does not change the other book's data.

A final attribute

final prevents assigning another value to the attribute:

public class Knyga {
    final String isbn = "978-609-01-0000-0";

    public static void main(String[] args) {
        Knyga knyga = new Knyga();

        knyga.isbn = "978-609-01-1111-1";
    }
}
Compiler result

error: cannot assign a value to final variable isbn

  1. The attribute isbn is marked with the keyword final.
  2. Its initial ISBN value is assigned when it is declared.
  3. After creating the object, the code tries to assign a different string to the same attribute.
  4. Java does not allow changing a final value, so the program does not compile.
  5. ISBN is a good example of a final attribute because the identifier of a specific book edition should not change.

An attribute and a local variable

public class Knyga {
    String pavadinimas = "Mažasis princas"; // Class attribute

    void skaityti() {
        int skaitymoTrukme = 20; // Local variable
        System.out.println(pavadinimas + ": " + skaitymoTrukme + " minučių");
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.skaityti();
    }
}
Console output

Mažasis princas: 20 minučių

  1. pavadinimas is declared in the class outside methods, so it is an attribute.
  2. This attribute belongs to a specific Knyga object.
  3. skaitymoTrukme is created inside the method skaityti() , so it is a local variable.
  4. The local variable exists only while this method invocation is executing.
  5. The method combines the object's attribute and a local value into one console line.
The difference between a class attribute and a local variable
PropertyClass attributeLocal variable
DeclaredIn a class, outside methodsInside a method or code block
Belongs toThe object's stateA particular method execution
AccessibleIn class methods or through an object, according to access rulesOnly in its declaration block
Default valueAssigned automaticallyMust be initialized before use

Protecting attributes

In these introductory examples, attributes are accessed directly. In practical object-oriented programs, they are often marked privateand their values are managed through methods. This is called .