Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Classes and objects

So far data and methods have lived separately. An object brings them together: it stores data and knows how to work with it. This approach is called object-oriented programming, or OOP. Java is built on it from top to bottom.

Class and object

A class is a blueprint, and an object is a thing built from it. The class describes what data an object will have, and the object itself is created with the new operator:

class User {
    String name;
}
 
void main() {
    User user = new User();
    user.name = "Anna";
    IO.println(user.name);
}

Output:

Anna

Variables inside a class are called fields. You access an object's field with a dot. A class can be declared in the same file as main — that way every example in this chapter stays a single file that is easy to run.

Methods

A method declared inside a class works with the data of its object. Fields are available in it simply by name:

class User {
    String name;
 
    String greet() {
        return "Hello, " + name + "!";
    }
}
 
void main() {
    User user = new User();
    user.name = "Anna";
    IO.println(user.greet());
}

Output:

Hello, Anna!

A method is called on a specific object, and it sees the fields of that very object.

Objects are independent

You can create as many objects from one class as you like. Each has its own set of fields:

class User {
    String name;
}
 
void main() {
    User first = new User();
    User second = new User();
    first.name = "Anna";
    second.name = "Oleg";
    IO.println(first.name);
    IO.println(second.name);
}

Output:

Anna
Oleg

Default values

If nothing has been assigned to a field, it has a default value: 0 for numbers, false for boolean and null for reference types such as String. The word null means "there is no object here":

class Product {
    String title;
    int price;
    boolean inStock;
}
 
void main() {
    Product p = new Product();
    IO.println(p.title);
    IO.println(p.price);
    IO.println(p.inStock);
}

Output:

null
0
false

Local variables inside methods do not get such a value — you must assign them before use.

A variable holds a reference

A variable does not contain the object itself but a reference to it — its address in memory. After an assignment both variables point to the same object:

class User {
    String name;
}
 
void main() {
    User a = new User();
    a.name = "Anna";
    User b = a;
    b.name = "Oleg";
    IO.println(a.name);
}

Output:

Oleg

No copy of the object appeared: a change made through b is visible through a too.

Common mistake

If a variable holds null, you cannot read a field or call a method on it — there is simply no object. The program compiles but crashes at run time:

class User {
    String name;
}
 
void main() {
    User user = null;
    IO.println(user.name);
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read field "name" because "<local1>" is null

<local1> is how the JVM refers to the local variable user when variable names were not kept during compilation. Before using an object, make sure it was created with new.

Thanks for reading!

Donate to the site — we work hard for you.