Q1: What are the class loaders in Java?
Answer:
Do you know how the very first class gets loaded in JVM? The first class is loaded with the help of main() method in the Java class. Once first class is loaded, the subsequent classes are loaded by other classes. All JVMs include one class loader called the bootstrap class loader. The JVM also includes the user defined class loader which helps to load classes in a particular order.
- Class loaders are hierarchical.
- These class loaders use a delegation model when loading a class in JVM.
- Child class loader requests its parent to load the class first before attempting to load it themselves.
- Once class is loaded in JVM, child class loader will not load it again.
- Classes loaded by the parent class loader will not have any visibility into classes loaded by its child.
- However, classes loaded by a child class loader have visibility in the parent class loader.
The types of class loader are mentioned below:
a) Bootstrap: Loads JDK internal classes, java.* packages. (rt.jar and i18n.jar)
b) Extensions: Loads jar files from JDK extensions directory (classes in the lib/ext directory of JRE)
c) System: Loads classes from system classpath (CLASSPATH environment variable or –classpath or –cp command line options)
- Classes loaded by the Bootstrap class loader have no visibility into classes loaded by the Extensions and Systems class loaders or any other child class loader.
- The classes loaded by System class loader have visibility into classes loaded by Extensions and Bootstrap class loaders, but they will not have visibility in classes loaded by Class loader 1 or Class loader 2.
- If there are any sibling class loaders they cannot see classes loaded by each other.
Q2: What is static class loader in Java?
Answer:
- Creating objects and instance using new keyword is known as static class loading
- The retrieval of class definition and instantiation of the object is done at the compile time.
- Classes are statically loaded with “new” operator in Java as
class MyTestClass {
public static void main(String args[]) {
Shape shape = new Shape();
}
If a class is referenced with “new” operator but the runtime system cannot find the referenced class then NoClassDefFoundException exception is thrown.
Q3: What is dynamic class loader in Java?
Answer:
- Loading classes use Class.forName () method.
- Dynamic class loading is done when the name of the class is not known at compile time. e.g.
Class oclass = Class.forName (String className); //It is static method which returns a Class
In the example below, the dynamic loading will decide whether to load the class Shape or
the class Triangle at runtime based on runtime conditions. Once the class is dynamically loaded the following method returns an instance of the loaded class.
Triangle otriangle = null ;
String myClassName = "com.abc.Triangle" ; // can be read at runtime
Class shapeClass = Class.forName(myClassName) ;
otriangle = (Triangle) shapeClass.newInstance();
otriangle.getArea();
If no definition for the class with the specified name could be found then ClassNotFoundException exception will be thrown for methods mentioned below:
- forName(…)- Class.
- findSystemClass(…)- ClassLoader.
- loadClass(…) - ClassLoader
Q4: What is constructor in Java?
Answer: A constructor in Java is a block of code similar to a method which is used to initialize the object of a class.
- It is called when an instance of an object is created.
- It cannot be static, final, abstract, final and synchronised.
- It does not have return type.
- It must have the same name as the class name.
- It is called only once per creation of an object.
e.g.
Pet.class
public Pet() {} // constructor
Answer:
- Explicit constructor is not required in the Java class.
- The Java compiler will create a default constructor in .class file with an empty argument, if we do not provide the constructor.
- The definition of default constructor looks like as "Country(){}".
- Java compiler does not create default constructor, if a class includes one or more explicit constructors like "public Country(int id)" or "Country(){}" etc.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.