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;
}Only the class attributes are defined here. No object is created and there is no println(), so nothing is printed.
pavadinimasisStringattribute, so it stores text.puslapiuSkaiciusisintattribute, so it stores an integer.perskaitytaisbooleanattribute, so its value can only betrueorfalse.- All attributes are written inside the class but outside methods.
- Every future
Knygaobject 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);
}
}Mažasis princas
96
- With
new Knyga()creates a specific book object. - The variable
knygalets you access this particular object. knyga.pavadinimasuses a dot to access the object's text field.knyga.puslapiuSkaiciusaccesses its numeric field.- 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);
}
}Pepė Ilgakojinė
true
- The new object initially has the title Mažasis princas and the value
false. knyga.pavadinimas = "Pepė Ilgakojinė"replaces the old text with new text.knyga.perskaityta = truemarks the book as read.println()displays the updated values rather than the initial ones.- The changes belong to this particular
knygaobject.
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);
}
}Mažasis princas: 96
Pepė Ilgakojinė: 160
- The same
Knygaclass creates two separate objects. - The first object is assigned one title and
96pages. - The second object is assigned another title and
160pages. - Each object has its own separate field values.
- 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";
}
}error: cannot assign a value to final variable isbn
- The attribute
isbnis marked with the keywordfinal. - Its initial ISBN value is assigned when it is declared.
- After creating the object, the code tries to assign a different string to the same attribute.
- Java does not allow changing a
finalvalue, so the program does not compile. - 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();
}
}Mažasis princas: 20 minučių
pavadinimasis declared in the class outside methods, so it is an attribute.- This attribute belongs to a specific
Knygaobject. skaitymoTrukmeis created inside the methodskaityti(), so it is a local variable.- The local variable exists only while this method invocation is executing.
- The method combines the object's attribute and a local value into one console line.
| Property | Class attribute | Local variable |
|---|---|---|
| Declared | In a class, outside methods | Inside a method or code block |
| Belongs to | The object's state | A particular method execution |
| Accessible | In class methods or through an object, according to access rules | Only in its declaration block |
| Default value | Assigned automatically | Must 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