Java Class Attributes

Attributes - also called fields or instance variables - are the pieces of data an object stores. They describe the state of an object: a Car's brand and year, a Person's name and age. In this lesson you will learn how to declare attributes, read and change their values, give them default or initial values, and lock them down with the final keyword.

Declaring attributes

An attribute is simply a variable declared directly inside a class, outside of any method. Each attribute has a type and a name. Once the class defines an attribute, every object created from that class gets its own copy of it.

A class with attributes

public class Person {
  String name;
  int age;
  boolean isStudent;
}

Reading and changing attribute values

After creating an object you access its attributes with the dot operator. You can read a value to use it, or assign a new value to change it. Because each object is independent, you set the attributes of one object without affecting any other.

Setting and reading attributes

public class Main {
  public static void main(String[] args) {
    Person p = new Person();
    p.name = "Aisha";
    p.age = 27;
    p.isStudent = false;

    System.out.println(p.name + " is " + p.age);  // Aisha is 27

    p.age = 28;  // change the value
    System.out.println(p.age);  // 28
  }
}

Default and initial values

If you create an object without setting its attributes, Java does not leave them as garbage. Every attribute gets a sensible default: numbers become 0, booleans become false, and object references (including String) become null. You can also give an attribute an initial value right where you declare it, and that value applies to every new object unless it is changed afterwards.

Initial values in the declaration

public class Account {
  String owner;          // defaults to null
  double balance = 0.0;  // initial value for every new Account
  String currency = "USD";
}
Attribute typeDefault value
int, long, short, byte0
double, float0.0
booleanfalse
char'\u0000' (the null character)
Objects (String, arrays, etc.)null

Constant attributes with final

Sometimes an attribute should never change once it has been set - a maximum limit, a fixed rate, or an ID. Marking it with the 'final' keyword makes it a constant: the compiler will reject any attempt to reassign it after its initial value is set. Final attributes that are also shared across all objects are usually combined with 'static' and written in UPPER_CASE by convention.

A final (constant) attribute

public class Circle {
  final double PI = 3.14159;  // cannot be reassigned
  double radius;
}

public class Main {
  public static void main(String[] args) {
    Circle c = new Circle();
    c.radius = 5;
    System.out.println(c.PI * c.radius * c.radius);  // 78.53975
    // c.PI = 3.14;  // ERROR: cannot assign a value to final variable
  }
}
  • An attribute is a variable declared inside a class but outside any method.
  • Access attributes through an object with the dot operator.
  • Unset attributes take a default value based on their type, never a random one.
  • Use 'final' to make an attribute a constant that cannot be changed after it is set.
Note: Object attributes default to null, not to an empty value. Calling a method on a null attribute - for example printing the length of a String that was never set - throws a NullPointerException at run time. Always set object attributes before you use them.