|
JAVA Interview Questions 6 - 10
|
Back to the list of all Java Interview Questions
|
|
Question 6: Explain the difference between static class loading and dynamic class loading?
|
|
Static class loading
|
|
Classes are statically loaded with Java’s “new” operator.
|
|
class
MyClass
{
public
static void main(String
args[])
{
Car c =
new Car();
}
}
|
A NoClassDefFoundException is
thrown if a class is referenced with
Java’s “new” operator (i.e. static loading)
but the runtime system cannot find the
referenced class.
|
|
Dynamic class loading
|
Dynamic loading is a technique for programmatically invoking the functions of a
class loader at run time. Let us look at how to load classes dynamically.
Class.forName (String className); //static method which returns a Class
The above static method returns the class object associated with the class
name. The string className can be supplied dynamically at run time. Unlike the
static loading, the dynamic loading will decide whether to load the class Car or
the class Jeep at runtime based on a properties file and/or other runtime
conditions. Once the class is dynamically loaded the following method returns an
instance of the loaded class. It’s just like creating a class object with no
arguments.
class.newInstance (); //A non-static method, which creates an instance of a class
(i.e. creates an object).
Jeep myJeep
= null ;
//myClassName should be read from a .properties file or a Constants class.
//stay away from hard coding values in your program. CO
String
myClassName = "au.com.Jeep" ;
Class vehicleClass
= Class.forName(myClassName) ;
myJeep
= (Jeep) vehicleClass.newInstance();
myJeep.setFuelCapacity(50);
A ClassNotFoundException is thrown when an application tries to load in a
class through its string name using the following methods but no definition for the
class with the specified name could be found:
|
- The forName(..) method in class - Class.
- The findSystemClass(..) method in class - ClassLoader.
- The loadClass(..) method in class - ClassLoader.
|
|
|
Question 7: What are “static initializers” or “static blocks with no function names”?
|
|
When a class is loaded, all blocks
that are declared static and don’t have function name (i.e. static initializers) are executed even before the
constructors are executed. As the name suggests they are typically used to initialize static fields.
|
|
public
class StaticInitializer
{
public
static final int
A = 5;
public
static final int
B; //note that it is not public static final int B = null;
//note that
since B is final, it can be initialized only once.
//Static initializer
block, which is executed only once when the class is loaded.
static
{
if(A == 5)
B = 10;
else
B = 5;
}
public
StaticInitializer(){} //constructor is called only after
static initializer block
} |
|
The following code gives an Output of A=5, B=10.
|
|
public
class Test
{
System.out.println("A =" + StaticInitializer.A +
", B =" + StaticInitializer.B);
} |
|
|
Question 8: What is the difference between constructors and other regular methods? What happens if you do not provide a
constructor? Can you call one constructor from another? How do you call the superclass’s constructor?
|
| Constructors |
Regular Methods |
Constructors must have the same name as the class name and cannot return a value.
The constructors are called only once per creation of an object while regular methods
can be called many times. E.g. for a Pet.class
public Pet() {} // constructor |
Regular methods can have any name and can be called any number of times. E.g. for
a Pet.class.
public void Pet(){} // regular method has a void return type.
Note: method name is shown starting with an uppercase to differentiate
a constructor from a regular method. Better naming convention is to have a meaningful
name starting with a lowercase like:
public void createPet(){} // regular method has a void return type |
|
|
Question 9: What happens if you do not provide a constructor?
|
Java does not actually require an explicit constructor in
the class description. If you do not include a constructor, the Java compiler will create a default constructor in the
byte code with an empty argument. This default constructor is equivalent to the explicit “Pet(){}”. If a class includes
one or more explicit constructors like “public Pet(int id)” or “Pet(){}” etc, the java compiler does not create the
default constructor “Pet(){}”.
|
|
Question 10: Can you call one constructor from another?
|
|
Yes, by using this() syntax. E.g.
|
|
public
Pet(int id)
{
this.id
= id; // “this”
means this object
}
public
Pet (int id, String type)
{
this(id);
// calls constructor public Pet(int id)
this.type
= type; //
”this” means this object
} |
Back to the list of all Java Interview Questions
|