Last Updated on July 9, 2023 by KnownSense
41. What is Static block in java?
Static block in Java is mainly used to initialize the static data members. The specialty of the static block is that it is executed before the main method at the time of class loading.
An example of the static block is as follows:
Class Mindmajix{
static{System.out.println("static block");
}
public static void main(String args[]){
System.out.println("Hello World");
}
}
42. Can we execute a program without main method in java?
Yes, we can execute the program in Java without the main method using a static block. But it was possible only till JDK 1.6 and from JDK 1.7 it is not possible to execute the program without the main method in Java.
43. What if we remove static modifier from main() method?
The program will be compiled, but at runtime, it throws NoSuchMethodError error.
44. Can we declare a constructor using static?
As we know that the static context is suitable for only class, variable, and method, not for the object. So the constructors are invoked only when an object is created, so there is no possibility to declare the constructor as static in Java
45. What is the use of this keyword in java?
The following are the usage provided by this keyword in Java, and they are:
- It is used to refer to the current class instance variable.
- This is also used to invoke the current class constructor
- It is used to invoke the current class method
- It can be passed as an argument in the method call and in the constructor call
46. How to make singleton class in java with example?
The most popular approach is to implement a Singleton by creating a regular class and making sure it has:
- A private constructor
- A static field containing its only instance
- A static factory method for obtaining the instance
public class CodingSingleton {
private static volatile CodingSingleton instance;
public static CodingSingleton getInstance() {
if (instance == null) {
synchronized (CodingSingleton .class) {
if (instance == null) {
instance = new CodingSingleton();
}
}
}
return instance;
}
// private constructor and other methods...
private CodingSingleton(){
// preventing Singleton object instantiation from outside
}
}
47. Can static method be overridden?
No, a static method cannot be overridden, it is because a static method is bounded with a class and an instance method is bounded with an object. So, the static belongs to the class area whereas, the instance method belongs to the heap area.
48. What is the difference between path and class path variables?
PATH is an environment variable used by the operating system to locate the executables. That’s why when we install Java or want any executable to be found by OS, we need to add the directory location in the PATH variable.
Classpath is specific to Java and used by java executables to locate class files. We can provide the classpath location while running the java application and it can be a directory, ZIP files, JAR files, etc.
49. What is a marker interface?
A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well-known marker interfaces are Serializable and Cloneable.
50. What are ENUM in java?
Enum was introduced in Java 1.5 as a new type whose fields consist of a fixed set of constants. For example, in Java, we can create Direction as an enum with fixed fields as EAST, WEST, NORTH, SOUTH.
Enum is the keyword to create an enum type and similar to the class. Enum constants are implicitly static and final
public enum Directions{
EAST,
WEST,
NORTH,
SOUTH
}
51. What is Reflection API in java and why is it important?
Java Reflection API provides the ability to inspect and modify the runtime behavior of java application. We can inspect a java class, interface, enum and get their methods and field details. Reflection API is an advanced topic and we should avoid it in normal programming. Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.
Even though we don’t use Reflection API in normal programming, it’s very important to have. We can’t have any frameworks such as Spring, Hibernate or servers such as Tomcat, JBoss without Reflection API. They invoke the appropriate methods and instantiate classes through reflection API and use it a lot for other processing.
Authored by codingknownsense.com