Use the keyword new to create an object from a class. Use the dot operator to reach attributes and methods.
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();
Student s1declares a reference variable that can point to a Student.new Student()creates the object in memory.=makes the variable refer to that object.
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.
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:
- Assignment copies the reference, not the object. After
Student s3 = s1;there is still only one object, with two names. ==compares references.s1 == s3is true because they point to the same object. Two separate objects with the same name and marks are not==. To compare contents you write anequals()method (covered with overriding in 3.5.4).- A reference can point to nothing.
Student s4 = null;means s4 refers to no object yet. Usings4.display()throws aNullPointerException.
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 objectMany objects: arrays of objects
You often need many objects of the same class. An array of a class type holds references:
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
newcreates 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?".