This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class loading

 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please explain me class loading phase in java.also
when class loading starts?
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arunseege paramashivappa wrote:Can anyone please explain me class loading phase in java.also
when class loading starts?

That's too big a topic to cover in a single post. My suggestion: look at this or this.

Winston
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks winston.But again i will come back with lot of questions...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch after 1½ years.
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class loading starts at compile time or runtime.m confused with class loading phase and object creation phase .
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, classes aren't loaded at compile time. The compiler simply turns your Java source code into Java bytecode, to be run at some future time. At that future time, when you run the program, that's when classes are loaded.

You can't create an object of class X until you have loaded class X. Does that answer your question about the phases?
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can find a lot of information here.
Compilation generates only the .class files (bytecodes). The loading starts when we invoke java command, and then an object/instance of java.lang.Class type is created into which the static members of the class that you execute (java ClassName) get memory, and from thereon, the heap memory, the stacks area (where methods exeute) are created depending upon your program logic and thus follows the processing of your coding logic.

Thank you
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.thanks rajdeep.So class loading starts whenever we say Animal a = new Animal().
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More precisely, when the JVM first encounters a mention of the Animal class.
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when Class Lion extends Class Animal.

when we say java Lion,when superclass get loaded.

Per discussions went earlier class gets loaded when class name encounters.Since Animal is superclass of Lion it has to get loaded ,but when it will be loaded...
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a hint: Assume that the people who designed Java were reasonably competent and knew what they were doing.

Now, you correctly point out that you can't load the Lion class until the Animal class has been loaded. So when you try to load Lion, you need Animal to be loaded. So what will happen?
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi paul,
what question m asking exactly is, in a class hierarchy when the superclass get loaded?.
In the Lion Animal inheritence tree, when i say,

java Lion

will all the classes(super classes) in the inherence tree get loaded or just Lion class get loaded.



 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You couldn’t get a better hint than what Paul C has already told you. What do you think will happen?
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Think all the classes in The inheritence tree gets loaded .after saying Java Lion suppose jvm encounters Lion lion=new Lion() ,constructor chaining starts ,at that time it needs super class already loaded .this is my understanding,please correct me if m wrong.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java Language Specification’s (JLS) version, and I think you will find you are correct

But the JLS is not easy to read.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic