Unit 3 · OOP Concepts

3.1.3Creating objects and accessing members

Use the keyword new to create an object from a class. Use the dot operator to reach attributes and methods.

Java
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();   // create object
        s1.name = "Nimal";            // access attribute
        s1.marks = 80;

        Student s2 = new Student();
        s2.name = "Kamala";
        s2.marks = 60;

        s1.display();                 // Nimal scored 80
        System.out.println(s2.getGrade());   // B
    }
}

In Student s1 = new Student();

1Student s1 declares a reference→2new allocates memory on the heap→3The constructor sets the attributes→4s1 stores the object's address

Two references can point to the same object. After Student s3 = s1; changing s3.marks also changes s1.marks, because both refer to one object.

referencesobjectss1s2s3Studentname"Nimal"marks80Studentname"Kamala"marks60s3 = s1s3.marks = 90 also changes s1.marks: there is only one object
Reference variables point to objects. After s3 = s1, both names refer to the same object

The dot operator

The dot (.) means "go into this object and use this member". s1.marks is "the marks of the object that s1 refers to", and s1.display() is "ask the object that s1 refers to to display itself". The same method display() gives different output for s1 and s2, because each object has its own attribute values.

A reference is not the object

A variable of a class type does not contain the object. It contains a reference: something like the object's address in memory. This has important effects:

Java
Student a = new Student();
a.name = "Nimal";
Student b = new Student();
b.name = "Nimal";

System.out.println(a == b);   // false: two different objects
Student c = a;
System.out.println(a == c);   // true: the same object

Many objects: arrays of objects

You often need many objects of the same class. An array of a class type holds references:

Java
Student[] group = new Student[3];   // three empty slots, all null
group[0] = new Student();           // now slot 0 refers to an object
group[0].name = "Nimal";

new Student[3] creates the array, but no Student objects: each slot starts as null until you fill it with new Student(). Forgetting this is a very common cause of NullPointerException.

Printing an object

If you write System.out.println(s1); Java prints something like Student@1b6d3586: the class name and a code derived from the object's identity, not its data. To print something useful, give the class a toString() method (see 3.5.4).

Public attributes, for now: the examples on this page set attributes directly (s1.marks = 80) to keep things simple. In real programs attributes are made private and changed through methods that check the values. You will learn how in 3.4 Encapsulation.

Remember

  • new creates the object; the variable only holds a reference to it.
  • Assigning a reference does not copy the object.
  • == on objects asks "same object?", not "same values?".