7

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"?

  • I edited the question to make it mor up to standard and on-topic. I believe many people could benefit frm reopening this question. – Tulains Córdova Jul 7 '17 at 14:53
9
  • 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
  • 1
    to add to this good write-up: an object is an object, but it is often also referred to as an "instance of the class". also, you say a variable is a reference to an object: is this a java thing or an OO thing? I primarily work with C++ where this is not exactly true. – blue Jul 13 '17 at 18:29
  • @blue It's a Java thing. In Java, pointers are under the hood but one knows the variable is really a reference to an object in memory (several variables can point to the same object). I guess in C++ they are pointers and pointers are references. Am I wrong? – Tulains Córdova Jul 13 '17 at 18:35
  • uhm..it is hard for me to understand what you mean with "pointers are references" :) In C++ we have things called values, references and pointers: Thing t instantiates a t value. Thing t2 = t instantiates a completely separate object (equal to t, but with a different address). Thing& t3 = t is a reference to t: basically an alias for t, if you ask for the address of t and t3 they are the same. All these objects were allocated on the stack and methods were called like t.Method. Thing* tp = new Thing allocates a Thing on the heap and methods are called like t->Method – blue Jul 13 '17 at 18:40
6

When one is asked to instantiate a class, is this another way of saying create an object for that class?

It is saying create an object of that class's type. In most object-oriented languages, classes are types. "Instantiating a class" is not quite accurate, but probably means "instantiate an object of the type defined by the class." For example, string is a common class in OO languages. Instantiating a string means "instantiate an object of type string."

If so does that mean the object created is also called the instance variable?

The term "instance variable" typically means "state internal to an object." Using the string example, an object of type string is not an instance variable belonging to the string class. A string object might have a character array that is an instance variable holding the actual string data. The character array belongs to a specific instance of string: not to the class itself, not to other strings.

  • Instantiating a class means creating an instance of it (which is an object); instantiating an object means creating an instance of the object, but that's nonsensical. – immibis Jul 7 '17 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.