All About Class ClassLoader in Java

0
134
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class ClassLoader in Java

ClassLoader is an abstract class. The applications implement subclasses of Classloader in order to extend mechanism by which JVM dynamically loads classes. In general JVM loads classes from the local file system in a platform-dependent manner(directory defined by CLASSPATH), however, some classes may be originated from another source like network or by an application.

In short, this class defines the necessary hook for Java to load classes over the network, or from other sources. Normal applications do not need to use or subclass this class.

The method defineClass() converts an array of bytes into an instance of a class. Instances of this newly created /defined class can be created by using the newInstance() method of the class-Class. The methods and constructors of objects created by a ClassLoader may reference other classes.

To determine the class(es) referred to, the JVM calls the loadClass() method of the ClassLoader that originally created the class. If JVM only needs to determine if the class exists or if it does not exists, to know its superclass, the resolve flag is set to false. However if an instance of the class is being created or any of its methods are being called, the class must also be resolved.

In this case, the flag resolve is set to true and resolveClass() method is called.

The structure of the ClassLoader class is given below:

public abstract class java.lang.ClassLoader extends java.lang.Object{
//constructor
protected ClassLoader();//creates a new classloader and initializes it. if there is a security manager, it check by createClassLoader() method.It may result in a SecuryException if the current thread does not have permission to create a new ClassLoader.
//Methods:
// Protected Constructor
protected ClassLoader();
// Class Methods
public static final URL getSystemResource(String name);
public static final InputStream getSystemResourceAsStream(String name);
// Public Instance Methods
public URL getResource(String name);
public InputStream getResourceAsStream(String name);
public Class loadClass(String name) throws ClassNotFoundException;
// Protected Instance Methods
protected final Class defineClass(byte[] data, int offset, int length);
protected final Class defineClass(String name, byte[] data, int offset,int length);
protected final Class findLoadedClass(String name);
protected final Class findSystemClass(String name) throws ClassNotFoundException;
protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException;
protected final void resolveClass(Class c);
protected final void setSigners(Class cl, Object[] signers);
} 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here