Java Class Attributes
The variables declared inside a class, outside its methods, are called fields or attributes. In Classes and Objects every Dog had its own name and age. This topic looks at the different kinds of fields and when to use each.
Instance fields
A field without static is an instance field: every object (instance) of the class gets its own copy. Fields can have a starting value; without one they get a default (0, false or null):
class Account {
String owner;
double balance = 100.0;
boolean frozen;
}
public class Main {
public static void main(String[] args) {
Account a = new Account();
a.owner = "Lata";
Account b = new Account();
b.owner = "Vikram";
b.balance += 50;
System.out.println(a.owner + ": " + a.balance + ", frozen " + a.frozen);
System.out.println(b.owner + ": " + b.balance);
System.out.println(new Account().owner);
}
}Lata: 100.0, frozen false Vikram: 150.0 null
Every new account starts with a balance of 100.0, and changing Vikram's balance does not touch Lata's.
final fields
A final field can be given a value only once. With a starting value, it is the same for every object and can never change:
class Circle {
final double PI = 3.14159;
double radius;
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
c.radius = 2;
System.out.println(c.PI * c.radius * c.radius);
}
}12.56636
Writing c.PI = 3; would not compile. A final field without a starting value must get its value in a constructor, which lets each object have its own fixed value, such as an ID number.
static fields: one copy for the whole class
A static field belongs to the class, not to an object. There is only one copy, shared by all objects. You reach it through the class name. It is ideal for counters and shared settings:
class Visitor {
static int count = 0;
String name;
void enter() {
count++;
System.out.println(name + " is visitor number " + count);
}
}
public class Main {
public static void main(String[] args) {
Visitor v1 = new Visitor();
v1.name = "Ana";
Visitor v2 = new Visitor();
v2.name = "Ben";
v1.enter();
v2.enter();
System.out.println("Total visitors: " + Visitor.count);
}
}Ana is visitor number 1 Ben is visitor number 2 Total visitors: 2
Both objects update the same count. Compare this with the static int visits field in the Scope topic: that was a static field of Main.
Constants: static final
A field that is both static and final is a class-wide constant. By convention its name is in capital letters with underscores. The Java library is full of them, such as Math.PI and Integer.MAX_VALUE:
class Shop {
static final double GST_RATE = 0.18;
static final String NAME = "Corner Store";
}
public class Main {
public static void main(String[] args) {
double price = 500;
System.out.println(Shop.NAME);
System.out.println("With GST: " + price * (1 + Shop.GST_RATE));
}
}Corner Store With GST: 590.0
So far other classes could read and change any field directly. That is risky: nothing stops a.balance = -5000;. The Modifiers and Encapsulation topics show how to protect fields.
Practice exercises
- Write a class
Productwith aname, apriceand astockfield that starts at 10, create two products and print them. - Add a static field that counts how many products were created (increase it in a method
register()). - Add a
static finalfieldCURRENCY = "INR"and print prices with it.