0

In Java, what is the difference between instance of a class and Object of a class.

For a class A, Take a look :

line 1 : A a; // Declaring a reference variable of class A

Line 2 : a = new A();// Instantiating an object of class A ( An object/instance is created
on Right hand side of the equation)

So....can the line 2 also be : // Instantiating an instance of class A

which mean Instance and Object are absolutely the same thing ?

Please give an objective answer than subjective.

.So an instance and object is same ? No difference at all. An object is an instance of the class .... or an Instance is an object of a class....both are same ?

pzaenger
  • 11,381
  • 3
  • 45
  • 46
Navchetan
  • 153
  • 1
  • 2
  • 11
  • Please stop [reposting this question](http://stackoverflow.com/questions/3323330/difference-between-object-and-instance). – user207421 Aug 27 '14 at 20:01

2 Answers2

1

For all intents and purposes, object and instance can be used interchangeably, but the accepted answer at this link will give you a good understanding of how you should use the two: Difference between object and instance

Community
  • 1
  • 1
cph2117
  • 2,651
  • 1
  • 28
  • 41
0

Yes, I'd agree that "instance" and Object are the same thing.

1 : A a; // Declaring a reference variable of class A
2 : a = new A();// Instantiating an object of class A
3 : Object o = a;  // a is also an object

All instances in Java are also Objects, so they're the same thing. That's an is-a relationship. You can say that any instance in Java is-a Object. Objects are a type (class), and you can make instances of just type Object alone if you want.

Object x = new Object();

Classes are also objects.

4 : Class<A> atype = a.getClass();
5 : Object otype = atype;

So objects (instances) have-a Class, and classes are objects too. I think this is why things are so murky, all these words bear a very close relationship. Note all the things on the left hand side are also called reference types.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • None of the things on the left-hand side of your examples are primitives. Primitives are special data types: the built-in types like `int`, `byte`, `char`, etc; they're [not objects or subclasses of `Object`](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html). – Josh Aug 27 '14 at 20:04
  • Hmm, apparently references aren't primitives. I'm a little shocked, since they work exactly like primitives, but the Java specification makes them distinct. I'll amend my answer. – markspace Aug 27 '14 at 20:19
  • Well, to say they work "exactly like primitives" is, I think, a bit of an oversimplification. You can declare a variable to be either a primitive _or_ a reference type. You can create a new reference type (by writing a new class), but you can't create a new primitive. Primitives also don't support methods; you can't say `int a = 3; a.plus(3);` – Josh Aug 27 '14 at 20:25
  • primitives use operators. `int i = 0; i + 3` for example. So do reference types: `atype == otype`. primitives have values, so do references: null, or the value returned by the "new" operator. Java always passes both by value in a method argument. That primitives don't support methods might be considered a mistake in the language. :) – markspace Aug 27 '14 at 20:31
  • Using `==` on two reference types is comparing equality using the JVM hashcode (ie, a primitive); it's almost always preferable to use `.equals()`. Reference types only support a limited subset of operators; you can't overload the arithmetic or bitwise operators to work on reference types like you can with C++ objects. Objects (reference types) are not passed by value into a method; rather, the _references_ to those objects are passed by value. It's [a subtle distinction](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Josh Aug 27 '14 at 20:44
  • PS: I'm not trying to sound argumentative or pedantic; I just think these are important distinctions, especially if anyone stumbles across this in the future (unlikely, since the question is dying the death of a dupe...). – Josh Aug 27 '14 at 20:46