The difference between Classes, Objects, and Instances

214,975

Solution 1

Java (and any other programming language) is modeled in terms of types and values. At the theoretical level, a value is a representation for some quantum of information, and a type is a set of values. When we say value X is an instance of type Y, we are simply saying that X is a member of the set of values that is the type Y.

So that's what the term "instance" really means: it describes a relationship not a thing.

The type system of the Java programming language supports two kinds of types, primitive types and reference types. The reference types are further divided into the classes and array types. A Java object is an instance of a reference type.

An object is a class instance or an array. (JLS 4.3.1)

That's the type theoretic view.

In practice, most Java developers treat the words "instance" and "object" as synonyms. (And that includes me then I'm trying to explain something quickly.) And most developers use the word "value" rather than "instance" to refer to an instance of a primitive type.

Solution 2

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

This is easy to understand if you look at an example. For example, suppose you have a class House. Your own house is an object and is an instance of class House. Your sister's house is another object (another instance of class House).

// Class House describes what a house is
class House {
    // ...
}

// You can use class House to create objects (instances of class House)
House myHouse = new House();
House sistersHouse = new House();

The class House describes the concept of what a house is, and there are specific, concrete houses which are objects and instances of class House.

Note: This is exactly the same in Java as in all object oriented programming languages.

Solution 3

A class is basically a definition, and contains the object's code. An object is an instance of a class

for example if you say

String word = new String();

the class is the String class, which describes the object (instance) word.

When a class is declared, no memory is allocated so class is just a template.

When the object of the class is declared, memory is allocated.

Solution 4

I like Jesper's explanation in layman terms

By improvising examples from Jesper's answer,

class House {
// blue print for House Objects
}

class Car {
// blue print for Instances of Class Car 
}

House myHouse = new House();
Car myCar = new Car();

myHouse and myCar are objects

myHouse is an instance of House (relates Object-myHouse to its Class-House) myCar is an instance of Car

in short

"myHouse is an instance of Class House" which is same as saying "myHouse is an Object of type House"

Solution 5

Class is Data Type,You use this type to create object.

  • Instance is Logical but object is Physical means occupies some memory.

  • We can create an instance for abstract class as well as for interface, but we cannot create an
    object for those.

  • Object is instance of class and instance means representative of class i.e object.

  • Instance refers to Reference of an object.

  • Object is actually pointing to memory address of that instance.

  • You can’t pass instance over the layers but you can pass the object over the layers

  • You can’t store an instance but you can store an object

  • A single object can have more than one instance.

  • Instance will have the both class definition and the object definition where as in object it will have only the object definition.

Syntax of Object:

 Classname var=new Classname();

But for instance creation it returns only a pointer refering to an object, syntax is :

 Classname varname;
Share:
214,975

Related videos on Youtube

Pranjut
Author by

Pranjut

jack of all trades master of one...

Updated on June 13, 2020

Comments

  • Pranjut
    Pranjut about 4 years

    What is a class, an object and an instance in Java?

    • Pushkarraj Pujari
      Pushkarraj Pujari almost 7 years
      Check the following link
  • Pranjut
    Pranjut almost 15 years
    Did you mean that objects and instances are same?
  • Pranjut
    Pranjut almost 15 years
    Thanks david for the link. From the topics I got this Every real world things which have state and behaviour can be called as "object". And to classify these objects we use class(A class is the blueprint from which individual objects are created). And it says that, the objects of the class are instances. Now please someone tell me what is the differences between object and instance?Does this mean that object don't really exist in context of programming and instance represents object in it?
  • Stephen C
    Stephen C almost 15 years
    @Mustafa: I'm sorry to contradict you, but according to the JLS, an array is also an object in Java. And you'll find that the JLS does not define the term 'instance' at all. See my answer.
  • Stephen C
    Stephen C almost 15 years
    @Ryan: See my answer for the distinction between "instance" and "object". @Mustafa's answer and comment are (IMO) misleading.
  • Pranjut
    Pranjut almost 15 years
    I really appreciate your answer sir, and I almost got it, just one more question. We say-"Whenever the compiler hits the 0 argument constructor it creates a instance of a class.". In this context what is really created an instance or an object? An object I guess and we use the word "instance" as synonym.But it would kind, if you better confirm it.
  • Joachim Sauer
    Joachim Sauer over 14 years
    Strictly speaking someCar is a reference to a Car instance. The instance itself doesn't have a name.
  • Rune FS
    Rune FS about 9 years
    in the context of Java (and some other languages) your theoretical explanation is sound. -however there are language that would contradict your leading statement. Simula the origin of OO would be one but there are other. There exist several languages where types them self are represented by object and thus are values. Eg the type of an object in JS is a value
  • Stephen C
    Stephen C about 9 years
    @RuneFS - the "modelling" I am talking about is not about representation. It is theoretical. Yea, in some languages there are objects that denote types, or even that allow you to enumerate all of the instances of a type. But those properties augment the generic "modelling" rather than invalidating it.
  • Rune FS
    Rune FS about 9 years
    @StephenC well in that case it should be data and operations since modern day computing is build on the von neumann architecture which has no types
  • Stephen C
    Stephen C about 9 years
    @RuneFS - No ... I am talking about "type theory". It is a branch of mathematics. It is independent of the technology that is used to run programs. The closest type theory gets to computation is lambda calculus. (If you are interested, try and get hold of a copy of "Types and Programming Languages" by Benjamin C. Pierce.)
  • Rune FS
    Rune FS about 9 years
    I've never met values in type theory only terms and types but i've met values and types when talking about different paradigms and different type system which is what got me confused.
  • Stephen C
    Stephen C almost 8 years
    These are just words. You don't actually explain what the words mean. (And in fact, Java classes do NOT behave like templates, either in real life or in the way that the do in languages like Javascript, Python, Ruby, etc.)
  • Suraj Jain
    Suraj Jain over 7 years
    class House { // blue print for House Objects } class Car { // blue print for Instances of Class Car } House myHouse = House new(); Car myCar = Car new();
  • Suraj Jain
    Suraj Jain over 7 years
    myHouse and myCar are objects myHouse is an instance of House (relates Object-myHouse to its Class-House) myCar is an instance of Car. Is this true , please see the above answer by user2390183 is it correct or not ?
  • Suraj Jain
    Suraj Jain over 7 years
    Sir , Can You tell me is user2390183 answer below is correct or not , i liked his answer and just want to confirm that i got right or not >?
  • Stephen C
    Stephen C over 7 years
    @SurajJain - It is sort of correct, though there are a couple of things that could be read the wrong way. For example, saying that myHouse is an instance is technically incorrect. Actually, myHouse is a variable that refers to an instance of House.
  • Stephen C
    Stephen C over 7 years
    Space may be allocated when a class is "created". The space is required for any static variables defined by the class. (And "logical" versus "physical" is stretching it, since an object representation is only bit patterns help in some memory device.)
  • Stephen C
    Stephen C over 7 years
    Yes ... but analogies can also be confusing. If Animal is a class and Cat is an instance, what is my pet pussy cat "Fluffy"?
  • Stephen C
    Stephen C over 7 years
    @mustafabar - "When a class is declared, no memory is allocated so class is just a template." - And this isn't true either. Memory is allocated to (at least) represent the static variables of the class. (And for other things too that are related to the type identity of the class.)
  • Admin
    Admin over 7 years
    @StephenC what could be the other difference then?
  • Stephen C
    Stephen C over 7 years
    See my Answer. This question cannot be answered properly by just talking about differences.
  • Suraj Jain
    Suraj Jain over 7 years
    So correct me if i am wrong , i understand that class is a definition or a template , a blueprint type of thing , like if i make a class Human , I would in that have its elements as Height , weight and so on , Now if i make an object named suraj refer to the class person instance . Similarly i can have a class house and myhouse refers to the house instance. So myhouse and suraj are objects , and house and person are classes. But myhouse refers to` house` instance and suraj refers to house instance.
  • Suraj Jain
    Suraj Jain over 7 years
    Both are objects, difference is only in the relationship with class , that is the instance.
  • Stephen C
    Stephen C over 7 years
    You are making the same mistake as user2390183. You are treating variables as "names". They are not. Variables are "reference holders" that contain references to objects. Objects don't have intrinsic names. The references are the closest thing that there is to a "name" for an object, except that they don't have a constant representation. (The GC can move an object which changes the bit pattern used to represent the reference.)
  • kaya3
    kaya3 over 4 years
    I don't think it makes sense to say that an instance is not a "thing", just because it is an instance of something. Obviously instanceof is a relation between two things, but it does not make sense to say "X is an instance of Y" if "an instance" is not a thing that X can be. Consider: a house is a "thing", even though it is a house of bricks or a house of cards.
  • Stephen C
    Stephen C over 4 years
    Your opinion is noted :-) Feel free to vote for someone else's answer .... or write your own. But note that I am not basing my argument on an analogy with other English words. I am basing it on concepts of type theory. Admittedly, in a rather clumsy way.
  • Stephen C
    Stephen C over 3 years
    In fact, in the real world (and in type theory), Animal denotes the set of all animals, and Cat denotes the set of all cats. Cat is a subset of Animal not an instance of Animal.
  • Stephen C
    Stephen C about 3 years
    Objects in the real world are physical. Objects in a computer program are not physical. (You can't touch them. They don't obey the laws of physics. Etcetera. And even the bit patterns in memory are representations of objects ... not actual Java objects.)