Object Instantiation – Concepts of Instance Variables

object-oriented

I'm a Network Engineer and I'm learning software developing in my spare time. I have some doubts about some OOP concepts. I'm trying to get an understanding of what is the difference when these terms and phrases are used.

  • When one is asked to instantiate a class, is this another way of saying create an object for that class? If I'm wrong please explain what instatiating actually is.

  • Are the objects created by instantiating called "instance variable"?

Best Answer

  • Instantiating a class is creating a live object of the type of that class. The class itself is dormant. You have to "spawn" objects using that class as a template.
  • "If so does that mean the object created is also called the instance variable?" . No. An instance variable is a totally different thing. The object is an object. Usually you have a variable of the type of the class which is a reference to the object. An instance variable is a variable that lives inside the object and that can have different values for different objects (instances), as opposed to a class varible that have the same value for all instances.

Example (in Java):

enter image description here

public class Thing {
    public static String hairColor="blue";
    public static String suitColor="red";

    private String name;

    public Thing(String s){
        name=s;
    }

    public String getName(){
        return name;
    }

    public String getHairColor(){
        return hairColor;
    }

    public String getSuitColor(){
        return suitColor;
    }   

    public void causeTrouble(){
        // this is a method, this is what Things DO
    }
}

As is common knowledge, all Things have blue hair and red suits. So those are class variables. They are static. They have the same value for all instances. If the value of those variables were to be changed during runtime, they will change for all Things. Maybe we should make those class variables "final" also, to be sure the hair will always be blue and the suits will always be red.

But Things have different names. Name is an instance variable, they can have different values for different instances. If a name changes, it does so only for a particular instance of Thing.

Let's see a test:


public class Test {

    public static void main(String[] args) {

    Thing t1,t2; //declare two variables that can point to Things.
    // right now they are not pointing to anything, they are null.

    t1= new Thing("Thing One");  // spawn a new Thing object and make t1 point to it
    t2= new Thing("Thing Two"); // spawn a new Thing object and make t2 point to it

    // Now let's print info about each object
    System.out.println(t1.getName());
    System.out.println(t1.getHairColor());
    System.out.println(t1.getSuitColor());
    System.out.println(t1);
    System.out.println();
    System.out.println(t2.getName());
    System.out.println(t2.getHairColor());
    System.out.println(t2.getSuitColor());
    System.out.println(t2);

}

t1 and t2 are variables of type Thing. They are not Things, they only point to Things. Once instantiated, real living Things can be accessed with that variables. (Those are not instance variables, they are just variables).

Output:

Thing One
blue
red

Thing Two
blue
red