OBJECT-ORIENTED PROGRAMMING

Java class methods

Class methods describe objects' actions and behavior. They can read or change object attributes, accept parameters and return calculated values.

What is a class method?

A method is declared inside a class and has a name, parameter parentheses, a return type and a code block. This method describes a simple action of opening a book:

public class Knyga {
    void atversti() {
        System.out.println("Knyga atversta");
    }
}
There is no console output.

The method atversti() is defined but never called. Declaring a method does not run its code.

  1. The method is declared in the class Knyga itself.
  2. void means the method returns no calculated value.
  3. atversti is a method name that starts with a verb.
  4. Empty parentheses () indicate that the method needs no parameters.
  5. The code inside the braces runs only when we call the method.

Calling a method through an object

To call an instance method, first create an object, then use the dot operator:

public class Knyga {
    void atversti() {
        System.out.println("Knyga atversta");
    }

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

Knyga atversta

  1. With new Knyga() creates a specific book object.
  2. The variable knyga stores a reference to that object.
  3. The dot operator in the expression knyga.atversti() selects this object's method.
  4. The method executes its println() line, so the console displays Knyga atversta.

Methods control object state

An instance method directly accesses the same object's attributes:

public class Knyga {
    int perskaitytaPuslapiu = 0;
    boolean baigta = false;

    void perskaitytiDesimtPuslapiu() {
        perskaitytaPuslapiu += 10;

        if (perskaitytaPuslapiu >= 10) {
            baigta = true;
        }
    }

    void rodytiBusena() {
        System.out.println("Perskaityta: " + perskaitytaPuslapiu);
        System.out.println("Baigta: " + baigta);
    }

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

Perskaityta: 10
Baigta: true

  1. The new object's initial attributes are 0 pages read and false.
  2. The method perskaitytiDesimtPuslapiu() adds to the attribute the value 10.
  3. The condition checks the new value. Since it has reached 10, the attribute baigta is changed to true.
  4. The method rodytiBusena() reads the updated attributes of the same object.
  5. The console therefore displays 10 and true.

Class methods with parameters

Parameters let the same object method perform an action with different data:

public class Knyga {
    int perskaitytaPuslapiu = 0;

    void skaityti(int puslapiai) {
        perskaitytaPuslapiu += puslapiai;
    }

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

        System.out.println(knyga.perskaitytaPuslapiu);
    }
}
Console output

40

  1. The method skaityti() has the parameter puslapiai.
  2. The first call passes 25, so the object's attribute increases from 0 to 25.
  3. The second call passes 15 and adds another 15 to the current value.
  4. Both calls change the same knyga object, so we get 25 + 15 = 40.

Class methods with a return value

Instead of void , you can specify a result type and return a value using return:

public class Knyga {
    int perskaitytaPuslapiu = 96;
    int puslapiuSkaicius = 96;

    boolean arBaigta() {
        return perskaitytaPuslapiu >= puslapiuSkaicius;
    }

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

true

  1. The object has 96 pages read and a total of 96 pages.
  2. The method arBaigta() compares these two attribute values.
  3. Condition 96 >= 96 is true.
  4. return returns the Boolean value true.
  5. println() displays the method's returned result in the console.

The same method, different objects

Each object runs the method with its own attribute values:

public class Knyga {
    int perskaitytaPuslapiu = 0;

    void skaityti(int puslapiai) {
        perskaitytaPuslapiu += puslapiai;
    }

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

        pirma.skaityti(20);

        System.out.println("Pirma: " + pirma.perskaitytaPuslapiu);
        System.out.println("Antra: " + antra.perskaitytaPuslapiu);
    }
}
Console output

Pirma: 20
Antra: 0

  1. This creates two independent Knyga objects.
  2. Both initially have a read page count of 0.
  3. The method skaityti(20) is called only on the first object.
  4. The first object's attribute therefore becomes 20, while the second object's attribute remains 0.
  5. The method changes only the object on which it was called.
The method changes only the object on which it was called.

The first book's read page count increases, while the second book's state stays unchanged.

Instance and static methods

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

    void rodytiPavadinima() {
        System.out.println(pavadinimas);
    }

    static void rodytiBiblioteka() {
        System.out.println("Miesto biblioteka");
    }

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

        Knyga.rodytiBiblioteka();
    }
}
Console output

Mažasis princas
Miesto biblioteka

  1. rodytiPavadinima() is an instance method because it uses the specific object's attribute pavadinimas.
  2. It is therefore called through the object: knyga.rodytiPavadinima().
  3. rodytiBiblioteka() is marked static and does not belong to one specific book.
  4. It is called through the class name: Knyga.rodytiBiblioteka().
  5. The console first displays a particular book's title, then the shared library name.
The difference between instance and static methods
Method kindBelongs toTypical call
InstanceA specific objectknyga.rodytiPavadinima()
staticThe class itselfKnyga.rodytiBiblioteka()

static method cannot directly access a specific object's instance attributes because it is not associated with one object.

Creating clear methods

  • Give the method an action-based camelCase name.
  • Each method should have one clear responsibility.
  • Change object state only when it fits the method's purpose.
  • Split a complex method into smaller helper methods.
  • Choose precise types for parameters and return values.