aspose file tools*
The moose likes Java in General and the fly likes Classloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classloader" Watch "Classloader" New topic
Author

Classloader

Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Hi,
I have a very basic question on classloader. I have searched this forum and have these questions:
Defintion :Classloader is a java class which is responsible for loading a java class and making it available to JVM.

Question
1. What do we mean by "loading a java class". The java classes (ie .class files) are already in classpath. Isn't JVM smart itself to read the .class files and then execute them. Please clarify.
2. When the class (ie MyAccount.class file) is "loaded" , a java class of type "java.lang.Class" is created and that is used by JVM for executing the MyAccount.class. Why is this java.lang.Class required. I could not understand the reason.
3. What do we mean by "making it available". Does it mean that the class bytecode is provided to JVM to execute.

Any help on this will be greatly appreciated. I have read a number of of articles on net regarding classloaders. My confusion is on basic concept of "loading a .class file". I could not find any reference explaining this.

Thanks,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Isn't JVM smart itself to read the .class files and then execute them.

Yes it is - thanks to ClassLoaders :-)

When the class (ie MyAccount.class file) is "loaded" , a java class of type "java.lang.Class" is created and that is used by JVM for executing the MyAccount.class. Why is this java.lang.Class required. I could not understand the reason.

Not quite: an object of type MyAccount.class is created, through which the actual objects (of type MyAccount) are instantiated and managed. Note the difference between an instance of MyAccount.class and an instance of MyAccount.

What do we mean by "making it available". Does it mean that the class bytecode is provided to JVM to execute.

Yes.


Ping & DNS - updated with new look and Ping home screen widget
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108

Isn't JVM smart itself to read the .class files and then execute them.


Yes it is - thanks to ClassLoaders :-)

I didn't understand still the term "loading the class". What does it mean. An elaborate answer will greatly help me understand the concept.


Not quite: an object of type MyAccount.class is created, through which the actual objects (of type MyAccount) are instantiated and managed. Note the difference between an instance of MyAccount.class and an instance of MyAccount.


I didn't understand the above 2 lines. Can you please elaborate.
I read somewhere that when a class is loaded then the classloader creates a object of type "java.lang.Class". Isn't it true. Then what is the purpose of this java.lang.Class object.

Thanks for the help
Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32

Loading the class means loading a reference to the ".class" file and then using it instantiating the byte code translated class and then using this reference the JVM executes the rest mentioned in the bytecode. Hope it helps you understanding the concept.


Pankaj Kumarkk wrote:

Isn't JVM smart itself to read the .class files and then execute them.


Yes it is - thanks to ClassLoaders :-)

I didn't understand still the term "loading the class". What does it mean. An elaborate answer will greatly help me understand the concept.


Not quite: an object of type MyAccount.class is created, through which the actual objects (of type MyAccount) are instantiated and managed. Note the difference between an instance of MyAccount.class and an instance of MyAccount.


I didn't understand the above 2 lines. Can you please elaborate.
I read somewhere that when a class is loaded then the classloader creates a object of type "java.lang.Class". Isn't it true. Then what is the purpose of this java.lang.Class object.

Thanks for the help


Regards
Prince
---------------------------------------------------------
SCJP 6 , still a life long learner
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
Let's say the JVM finds a reference to the class named "MyClass". It then provides the String "MyClass" to a ClassLoader, which then 'finds' the bytecode associated with that name, and transforms it into an instance of Class<MyClass>.

Usually, bytecode is 'found' in .class files on the classpath. This is not necessary however! A custom class loader may also download the bytecode from an external server.

So by loading a class it is meant that a ClassLoader takes the name of a class in the form of a String, finds the bytecode associated with that name, and transforms it into an instance of java.lang.Class.
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Hi Stephan,
Thanks for the explanation. So when the object of type "Class" is created, then what is it used for?
How is this "Class" object useful to JVM.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

When you create a new instance of that class, the JVM needs to know all about the class so that it an create an instance.

If the JVM didn't have that info available, it'd be like asking you to build a house without the plans. The Class instance is the "plans" for the class needed to create an instance of it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Thanks for the response.
I think I now understand the concept of classloader better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classloader
 
Similar Threads
What is ClassLoader
JDBC Driver Classes JDK version issue
please clarify some doubts
Object creation in java
classloader