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.

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: 44016

33

posted Jul 16, 2009 04:10:00

0

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

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.

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: 44016

33

posted Jul 16, 2009 08:47:08

0

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.

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.

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

posted Jul 16, 2009 09:50:06

0

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 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.