This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

(Jacquie Barker, hi) difference between class instantiation and object instantiation?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was asked in an interview to give difference between class
instantiation and object instantiation?
i never thought them to be different and answered just this.
but to my surprise they said these two are different things.
i later tried to read ooad with apps(Booch) and found that
it also hinted at a similar thought, but unfortunately
that book is tough to understand and also it is more
focussed on c++ and discusses such things with c++ in
mind.
so at the end i only know that is some difference between
those two terms, but what it is, is still not known/clear.
i would like to take this oppurtunity to ask author
Jacquie Barker(this is a question of java objects).
hoping for an answer from anyone and everyone.

------------------
))
((
C|~~|
`--'
JAVA
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know,class is a template of object.object is an instance of a class.U can only instantiate class to get an Object which can be used in real world appli. But im not sure abt instantiating an object(which is already existing), can any one update me on this.
Anil
 
sandeep vijan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instantiating an object is what allows you to actually use objects in your program. You can write hundreds and hundreds of class declarations, but none of that code will be used until you create an instance of an object. A class declaration is merely a template for what an object should look like. When you instantiate an object, C++ follows the class declaration as if it were a blueprint for how to create an instance of that object.

There is a very important distinction between an object and an instance of an object. An object is actually a definition, or a template for instances of that object. An instance of an object is an actual thing that can be manipulated. For instance, we could define a Person object, which may include such member data as hair color, eye color, height, weight, etc. An instance of this object could be "Dave" and Dave has values for hair color, eye color, etc. This allows for multiple instances of an object to be created. Let's go back to the medieval video game example and define the monster object.
(chapter 6 and 7 at http://www.intap.net/~drw/cpp/index.htm)

------------------
. ))
. ((
. C|~~|
. `--'
. JAVA
 
author
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sandeep vijan:
i was asked in an interview to give difference between class
instantiation and object instantiation?
i never thought them to be different and answered just this.
but to my surprise they said these two are different things.
i later tried to read ooad with apps(Booch) and found that
it also hinted at a similar thought, but unfortunately
that book is tough to understand and also it is more
focussed on c++ and discusses such things with c++ in
mind.
so at the end i only know that is some difference between
those two terms, but what it is, is still not known/clear.
i would like to take this oppurtunity to ask author
Jacquie Barker(this is a question of java objects).
hoping for an answer from anyone and everyone.


Hmmm ... this sounds like fuzzy terminology to me. The term "instantiation" as I've always used it means "to create an instance/object based on a class definition/template". I have not heard of "object instantiation" as being different from "class instantiation". The only thing I can guess at is that perhaps when they say "class instantiation", they are referring to the point in time at which a class is first loaded into the Java Virtual Machine (?). That is, when you type
java MyProgram
at the command line, then the class loader loads the byte code contained in MyProgram.class into the JVM. (The JVM is also preloaded with all of the classes in the java.lang package.) Then, whenever your program first references a class by name -- say, Person -- then the JVM loads that class's byte code into the JVM.
Just as we write constructors that are used to control how an object is instantiated, there is also a technique that we can use for running code that is to be executed only once, when the class is loaded into the JVM.
Here's a simple example to illustrate this concept (sorry about the fact that the indentation is messed up):
public class Example {
int x;
// A static code block gets invoked only once, when
// a class is first loaded into the JVM.
// You can think of it as a
// "constructor" for the class as a whole!
static {
System.out.println("Loading the Example class ...");
}
// A "normal" constructor for (instances of) OBJECTS
// of type Example.
public Example() {
x = 1;
System.out.println("Instantiating an Example object ...");
}
public static void main(String[] args) {
Example a = new Example();
Example b = new Example();
Example c = new Example();
}
}
Compile and run this program, and you'll see the following output:
Loading the Example class ...
Instantiating an Example object ...
Instantiating an Example object ...
Instantiating an Example object ...
So, perhaps when they talk about class instantiation, this is what they are referring to ... ?
------------------
author of:
Beginning Java Objects
 
His brain is the size of a cherry pit! About the size of this ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic