aspose file tools*
The moose likes Java in General and the fly likes Class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class loading" Watch "Class loading" New topic
Author

Class loading

arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

Can anyone please explain me class loading phase in java.also
when class loading starts?


Thanks-Arun
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7823
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

thanks winston.But again i will come back with lot of questions...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Welcome to the Ranch after 1½ years.
arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

Class loading starts at compile time or runtime.m confused with class loading phase and object creation phase .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Mar 26, 2012
Posts: 186

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


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

Thanks Paul.thanks rajdeep.So class loading starts whenever we say Animal a = new Animal().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
More precisely, when the JVM first encounters a mention of the Animal class.
arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Greenhorn

Joined: Nov 02, 2010
Posts: 29

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

Joined: Oct 13, 2005
Posts: 38902
    
  23
You couldn’t get a better hint than what Paul C has already told you. What do you think will happen?
arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

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

Joined: Oct 13, 2005
Posts: 38902
    
  23
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.
 
 
subject: Class loading