Java Classes and Objects

Java is an object oriented language. A class is a blueprint, and an object is a real thing made from that blueprint. A class Car is the idea of a car, and your red Swift is an object of it.

Create a class

A class has fields (the data) and methods (the actions).

class Student {
    String name;          // fields
    int marks;

    void show() {         // method
        System.out.println(name + " scored " + marks);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();    // create an object
        s1.name = "Riya";
        s1.marks = 88;

        Student s2 = new Student();
        s2.name = "Aman";
        s2.marks = 72;

        s1.show();
        s2.show();
    }
}
Output

Riya scored 88
Aman scored 72

new Student() creates an object in memory. The dot . lets you use the fields and methods of the object. Every object has its own copy of the fields.

Default values of fields

class Box {
    int number;
    boolean open;
    String label;
    double size;
}

public class Defaults {
    public static void main(String[] args) {
        Box b = new Box();
        System.out.println(b.number + " " + b.open + " " + b.label + " " + b.size);
    }
}
Output

0 false null 0.0

References

A variable of a class type does not keep the object. It keeps a reference (the address). Two variables can point to the same object:

class Counter {
    int count;
}

public class Ref {
    public static void main(String[] args) {
        Counter a = new Counter();
        Counter b = a;               // same object, not a copy
        b.count = 10;
        System.out.println(a.count);

        Counter c = new Counter();   // a new object
        System.out.println(c.count);
        System.out.println(a == b);  // same object
        System.out.println(a == c);
    }
}
Output

10
0
true
false

null

A reference can be null, which means “no object”. Using it gives a NullPointerException.

class Pet {
    String name = "Tommy";
}

public class Null {
    static Pet p = null;

    public static void main(String[] args) {
        System.out.println(p == null);
        System.out.println(p.name);
    }
}
Output

true
Exception in thread "main" java.lang.NullPointerException: Cannot read field "name" because "Null.p" is null
at Null.main(Null.java:10)

The toString method

Printing an object shows its class name and a code. You can write a toString() method to print something useful:

class Point {
    int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point(" + x + ", " + y + ")";
    }
}

public class Str {
    public static void main(String[] args) {
        Point p = new Point(3, 4);
        System.out.println(p);
    }
}
Output

Point(3, 4)

Garbage collection

You do not delete objects. When no variable points to an object anymore, the garbage collector of the JVM frees its memory by itself.

Note: You can put many classes in one file, but only one can be public, and it must match the file name. The next chapter shows the constructors, which set up an object when it is created. Next: Java Constructors.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.