Distinguish between an object (definition, template or class) and instantiation.


Teaching Note:

Students must understand the difference in terms of code definitions, memory use and the potential creation of multiple instantiated objects.


Sample Question:


JSR Notes:


The point here is you make the class, and then you make instances of it. So my Student "template" class might be defined as:

public class Student{
    private String name = "not set yet";
    private int grade = -999;

    public Student(String name, int grade){
        this.name = name;
        this.grade = grade;


(i.e., above is the class definition.)


But then you use the template class by making instances of it, and potentially - in fact most likely - many instance of it.

So from a "main" etc. class:

Student s = new Student("Sally", 10);
Student s1 = new Student("George", 11);
Student s2 = new Student("John", 12);

(i.e. above are actual instances or objects of the template class.)

And each line above is indeed instantiation of a unique object of the Student class. Instantiation, then, is making a new instance of an object by creating new memory for an address which points to the object, and calls its constructor.



In terms of the "memory use" referred to in the teaching note, the original class definition itself takes up a limited amount of memory, but note that each individual object of the class which is instantiated takes up exactly that much memory again. i.e. each instance of a class has its own attributes (name and grade in the example above) along with its own methods (the constructor and gets and sets for example).