Mariano Garcia Jr.
Mariano Garcia Jr.

Reputation: 7

What do we call an instance of a class in Java

A question from my homework assignment asks

What do we call an instance of a class in Java?

The choices are:

A. method
B. Package
C. Object
D. Variable

I think it's a variable because an instance is another word for an object, and objects have variables. Is my answer correct?

Upvotes: 0

Views: 2025

Answers (3)

Ravindra babu
Ravindra babu

Reputation: 38910

Object is an instance of Class.

Class defines a group of Objects with state (data) and behaviour (methods to act on data)

From your options, method has been ruled out as methods are defined in class and belong to objects.

Variable can be anything : primitive types like int, boolean etc or Object of any Class type like Thread etc.

You can think class as logical entity and object as physical entity.

Man can be a class with attributes like name,age etc with methods like canWalk () and canThink ().

Object can be particular instance of Man with attribute values like Ravindra as name, 40 as age.

sample code:

public class Man{
    String name;
    int age;

    public Man(String name, int age){
        this.name = name;
        this.age = age;
    }
    public void canWalk(){
        System.out.println("Can walk");
    }
    public void canThink(){
        System.out.println("Can think");
    }
    public String toString(){
        return "Man:"+this.name+":"+age;
    }
    public static void main(String args[]){
        Man m1 = new Man("Ravindra", 40);
        Man m2 = new Man("Someoneelse",30);
        System.out.println(m1);
        System.out.println(m2);

    }
}

both m1 and m2 are objects, which are instances of class Man and they are different.

Upvotes: 0

sebasaenz
sebasaenz

Reputation: 1956

The answer would be Object. For me it's useful to think that a class is like a very general concept of something (e.g. the concept of a car) and the instance of a class is like the materialization of it, the "objectification" (a pick-up, a coupé, etc.).

Upvotes: 0

Alan Guo
Alan Guo

Reputation: 322

The definition of an object in object oriented programming is an instance of a class. So the answer should be C. Object.

A variable is any reference that doesn't necessarily have to be an instance of a class. It could be a primitive type, for example.

Upvotes: 3

Related Questions