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

Explain class object and instance

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Hi , i read definition of class , object and instance but still am not clear about this. can you explain with one example
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Have a look at this recent topic.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Colin Wright
Greenhorn

Joined: Apr 21, 2010
Posts: 8
I'll have a try; a class is like a template for an object, an object is an instance of a class.

As a real world example;

a Porsche 911 C2 S Cabriolet is a type (or class of car), there are many Porsches of this model on the road and you could think of each of them as an instance of that car and as an object.

Java is just like this a class is the description of a of the attributes and functionality of an object, it's like a blue print. But you can't use a blue print accept to create an object, you can however use the object much as you can use a car. Creating an Object from a class is refered to as creating an instance of the class), in thory you can make as many objects from one class as you like and use them all independently but they all function as described in the blue print (class).

It's a bit more complex in JAVA because of the static keyword, a static member or method of a class can be used direct from the blue print without making an object and a static member of a class shares it's value across all instances of that class.

An example:

I have a class called "Man"

It has two attributes defined; Height, Number of eyes (this is defined as static in the class)

I create two instances of the Man class call then Colin & Dave

I can set the height of Colin to 172cm and set the hieght of Dave to 100cm. They are seperate objects so they each have a seperate Height attribute because it's not static. "Man" does not have a value for Hieght because it's a blue print not a specific object.

Because "Number of Eyes" is defined as static I "Man" does have a value for that which it shares with Colin & Dave because they are instances of that class. They do not have their own copy of this attribute there is only 1 copy for all objects of the class as it's static. So if I change a static attribute it would effect all objects of that class in that have been created and all future objects of that class created.

Statics useful for values that will apply equally to all objects created from a class because it means you can change the attribute once and it will apply to all current and future objects of that type, it's also useful for constants because it saves storing a new copy of the attribute in memory every time you make a new object from the class. It's also useful for methods of a class you want to be able to use without making an object first.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain class object and instance