• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Creation before definition of Class Completes?  RSS feed

 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i just went through a simple code.. in which an OBJECT of a class is created even before the definition of its class is completed!!!
how is that possible??
For example,




//Here,
how is it possible to create the object, obj before we can complete the definition of the Main class ???
i never saw this happening in C/C++ ..... :\
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compilter is smart enough to deal with this case. Remember that the object doesn't actually get created at compile time. When you run javac, Java remembers what you want to do until it finishes compiling the class. This also allows you to declare methods/instance variables in any order.

By the time you get to runtime, the class is fully compiled and all is well.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The compilter is smart enough to deal with this case. Remember that the object doesn't actually get created at compile time. When you run javac, Java remembers what you want to do until it finishes compiling the class. This also allows you to declare methods/instance variables in any order.

By the time you get to runtime, the class is fully compiled and all is well.

what i expect is at run-time, since that object has been mentioned inside the class definition, so whenever that class gets instantiated, that object (here, obj i.e; the one inside the class) would recursively get created each time whenever that class gets instaniated... no?
also, please explain what you meant by saying that " This also allows you to declare methods/instance variables in any order. "
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:
what i expect is at run-time, since that object has been mentioned inside the class definition, so whenever that class gets instantiated, that object (here, obj i.e; the one inside the class) would recursively get created each time whenever that class gets instaniated... no?
also, please explain what you meant by saying that " This also allows you to declare methods/instance variables in any order. "


No. The main() method is static, and it is only ran once by the JVM on startup. Creating an instance of the Main class doesn't make the JVM call the main method again.


And I am not sure what you mean by "class completes"? Do you mean that the class definition completes loading? The JVM will load and initialize the class definition, before calling the main method.

Henry
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote: . . . whenever that class gets instantiated, that object (here, obj i.e; the one inside the class) would recursively get created each time whenever that class gets instaniated... no? . . .
No is correct. That object is a local variable in the main method. It is created when the main method executes. You can recursively create instances like thisTry running that and you can see what recursive creation looks like
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!