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");
}
}The method atversti() is defined but never called. Declaring a method does not run its code.
- The method is declared in the class
Knygaitself. voidmeans the method returns no calculated value.atverstiis a method name that starts with a verb.- Empty parentheses
()indicate that the method needs no parameters. - 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();
}
}Knyga atversta
- With
new Knyga()creates a specific book object. - The variable
knygastores a reference to that object. - The dot operator in the expression
knyga.atversti()selects this object's method. - 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();
}
}Perskaityta: 10
Baigta: true
- The new object's initial attributes are
0pages read andfalse. - The method
perskaitytiDesimtPuslapiu()adds to the attribute the value10. - The condition checks the new value. Since it has reached
10, the attributebaigtais changed totrue. - The method
rodytiBusena()reads the updated attributes of the same object. - The console therefore displays
10andtrue.
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);
}
}40
- The method
skaityti()has the parameterpuslapiai. - The first call passes
25, so the object's attribute increases from0to25. - The second call passes
15and adds another 15 to the current value. - Both calls change the same
knygaobject, so we get25 + 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());
}
}true
- The object has
96pages read and a total of96pages. - The method
arBaigta()compares these two attribute values. - Condition
96 >= 96is true. returnreturns the Boolean valuetrue.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);
}
}Pirma: 20
Antra: 0
- This creates two independent
Knygaobjects. - Both initially have a read page count of
0. - The method
skaityti(20)is called only on the first object. - The first object's attribute therefore becomes
20, while the second object's attribute remains0. - 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();
}
}Mažasis princas
Miesto biblioteka
rodytiPavadinima()is an instance method because it uses the specific object's attributepavadinimas.- It is therefore called through the object:
knyga.rodytiPavadinima(). rodytiBiblioteka()is markedstaticand does not belong to one specific book.- It is called through the class name:
Knyga.rodytiBiblioteka(). - The console first displays a particular book's title, then the shared library name.
| Method kind | Belongs to | Typical call |
|---|---|---|
| Instance | A specific object | knyga.rodytiPavadinima() |
static | The class itself | Knyga.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.