• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explain class object and instance

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , i read definition of class , object and instance but still am not clear about this. can you explain with one example
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this recent topic.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic