aspose file tools*
The moose likes Beginning Java and the fly likes Object Vs Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Vs Instance" Watch "Object Vs Instance" New topic
Author

Object Vs Instance

Soumya Bardhan
Greenhorn

Joined: Jul 14, 2009
Posts: 9
Can someone explain me d difference between an object and an instance.

Thank You
Soumya Bardhan
Frank Kellinghusen
Greenhorn

Joined: Jul 10, 2009
Posts: 11
Soumya Bardhan wrote:Can someone explain me d difference between an object and an instance.

Thank You
Soumya Bardhan


Maybe this helps:
http://faq.javaranch.com/java/ObjectVsInstance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
And welcome to JavaRanch, both of you
Soumya Bardhan
Greenhorn

Joined: Jul 14, 2009
Posts: 9
Thanks Frank i already that before i made the post .
I am new to java.
That concept is still a bit vague to me.
Could you please explain a bit more on that?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Soumya Bardhan wrote:Thanks Frank i already that before i made the post .
I am new to java.
That concept is still a bit vague to me.
Could you please explain a bit more on that?


How much time do you have?

Here's a bit of an elaboration.It's not the whole story, just an example. Consider the following java statement

Thing thing1;

here Thing is a class, a blueprint if you wish, that will be used to create one or more objects (instances). thing1 is a name that will be assigned to a specific object (instance) of class Thing when thing1 is created (Instantiated) as follows.

thing1 = new Thing();

once this happens, you can actually use this Thing called thing1 to do things. Such as displaying a string representation of thing1 using System.out.println( thing1.toString() );

Notice the two lines could be combined into one, with the same end result.

Thing thing1 = new Thing();

or you could type System.out.println( (new Thing()).toString() );. Here the object never has a name, but it existed just the same, but only long enough to execute the print statement, then that Thing without a name is gone.

In this example, thing1 is the name of an object that is an instance of class Thing.

Don't try to absorb to much too soon, whith experience will come greater undersatnding
Larrya Anglin
Greenhorn

Joined: Jul 15, 2009
Posts: 2
Tell me if I am wrong; but I tried to explain it to a friend and came up with this.
An object is like the words in your head waiting to be used.
When you speak, write, or type them; that is the instance of the words.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Larrya Anglin wrote:Tell me if I am wrong; but I tried to explain it to a friend and came up with this.
An object is like the words in your head waiting to be used.
When you speak, write, or type them; that is the instance of the words.
Not convinced. The words waiting in your head might represent the class "Sentence" and what you say is an instance of "Sentence".
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Larrya Anglin wrote:An object is like the words in your head waiting to be used.
When you speak, write, or type them; that is the instance of the words.

Hmmm, not really. It's really simple:

An object is an instance of a class.

"Object" and "instance" are really just different words for the same thing.
The word "instance" relates an object to its class - an object is an instance of its class.
A class is a blueprint for creating objects, and objects created from a class are called instances of that class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kevin McMahon
Greenhorn

Joined: Nov 03, 2008
Posts: 19
Lego.
The little instruction booklet that comes with the lego toys - that's the class - the definition of what's required to build an instance. So, for a lego helicopter, the instruction book is your Helicopter class. It's tells us (the compiler) what building blocks we need (what data structures) for the helicopter.

We can use those instuctions to build many actual lego helicopters. The actual helicopters are the instances.

(Yes, yes, I know this doesn't consider methods - things you can actually do to a helicopter, but you get my drift. Ok - I just like lego helicopters...)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
A Lego helicopter is a perfectly good instance. It can have attributes eg size, colour. It can have methods and behaviour, eg "fly" "fall in pieces when dropped from 10 feet" etc.

I still think Jesper's is the best answer.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Do you know what a car is?

A car is an object. Just saying the word 'car' conjures up all sorts of ideas about a car, be it how fast it goes (behavoir or methods) or how heavy a car might be (its property or state).

Can you describe a car to someone without actually showing them a real car? Of course you can! You can define or describe what a car is without holding one, touching one or driving one. An object is like a car, where you can define or describe it by the characteristics that the car has.

A car is an object, that has properties and behavior.

Do YOU have a car? Does it occupy space? Can you touch it, or drive it, or crash it? YOUR car is like an instance. It is a real thing that actually does all of the things we would expect a car to do. But it's different from other cars, becuase it is a unique instance.

So, in Java, an object is like the general idea of a car, where YOUR car would be equivalent to an instance.



With that code, you are saying you need something that is a Car. You just want an object to fulfill the requirement, and the requirement is a Car.


This actually creates a real, tangible instance of a car that can be manipulate on a JVM.

Well, at least, that's how I think about it.

-Cameron Mckenzie
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

or you could type System.out.println( (new Thing()).toString() );. Here the object never has a name, but it existed just the same, but only long enough to execute the print statement, then that Thing without a name is gone.

There's still an instance of Thing, though, for the duration of the s.o.p.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
David Newton wrote:
or you could type System.out.println( (new Thing()).toString() );. Here the object never has a name, but it existed just the same, but only long enough to execute the print statement, then that Thing without a name is gone.

There's still an instance of Thing, though, for the duration of the s.o.p.


What does s.o.p. mean? if you mean System.out.print, well that's exactly what I originally said, althought not in the same words.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh. I thought you were differentiating to explain object v. instance.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
David Newton wrote:Oh. I thought you were differentiating to explain object v. instance.


No, In the part you quoted, I meant the same thing, I could have said "The instance lasted long enough..." An object is an instance for the purpose of my post. Sorry of I wasn't clear.

Probably I clouded the issue a bit by differentiating the name of an object from the object itself, as I see it the name of the object exists more or less independantly of the object itself. You can have a name without an object and you can have a useful object without a name, although that second use would be limited.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Gotcha; my fault.

Although objects-without-names are used a lot (think anonymous inner classes).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23

Let's get back to the original thread subject.

An object is a software representation of something. An object is derived from a class and you call it an instance of that class. The difference is very slight.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Vs Instance