• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classloader

 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Stephan van Hulst
Bartender
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.
I think I now understand the concept of classloader better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic