Java Interview Questions
Interview Questions
Final, Static, This
Why a static block executes before the main method ?
A class has to be loaded in main memory before we start using it. Static block is executed during class loading. This is the reason why a static block executes before the main method.
Can we override static method ?
No, static method cannot be overridden.
Why we cannot override static method ?
because static method is bound with class where as instance method is bound with object. Static belongs to class area and instance belongs to heap area.
Why use this keyword
The main purpose of using this keyword is to differentiate the formal parameter and data members of class, whenever the formal parameter and data members of the class are similar then jvm get ambiguity (no clarity between formal parameter and member of the class)
To differentiate between formal parameter and data member of the class, the data member of the class must be preceded by "this".
When Need of super keyword ?
Whenever the derived class is inherits the base class features, there is a possibility that base class features are similar to derived class features and JVM gets an ambiguity. In order to differentiate between base class features and derived class features must be preceded by super keyword.
What is the difference between this. (this dot) and this() (this off).
this. can be used to differentiate variable of class and formal parameters of method or constructor.
this() can be used to call one constructor within the another constructor without creation of objects multiple time for the same class.
Difference between static and final keyword
static keyword always fixed the memory that means that will be located only once in the program where as final keyword always fixed the value that means it makes variable values constant.
why main method is static ?
Because object is not required to call static method if main() is non-static method, then jvm create object first then call main() method due to that face the problem of extra memory allocation.
Difference between non-static and static variable ?
Non-Static method | Static method | |
---|---|---|
1 | These method never be preceded by static keyword Example: void fun1() { ...... ...... } | These method always preceded by static keyword Example: static void fun2() { ...... ...... } |
2 | Memory is allocated multiple time whenever method is calling. | Memory is allocated only once at the time of loading. |
3 | It is specific to an object so that these are also known as instance method. | These are common to every object so that it is also known as member method or class method. |
4 | These methods always access with object reference Syntax: Objref.methodname(); | These property always access with class reference Syntax: className.methodname(); |
5 | If any method wants to be execute multiple time that can be declare as non static. | If any method wants to be execute only once in the program that can be declare as static . |
What is difference between super and this keyword
Super keyword is always pointing to base class features and this keyword is always pointing to current class features. This Keyword in Java
What is difference between super(), super(..), this() and this(..).
super() and super(..) are used for establishing the communication between base class and derived class constructor.
this() and this(...) are used for establishing the communication between current class constructor.
Which access specifiers is known as package access specifiers.
default access specifiers is known as package access specifiers.
Why abstract class not made as final ?
Abstract classes definitions should not be made as final because abstract classes always participate in inheritance classes.
Difference between non-static and static variable ?
Non-Static method | Static method | |
---|---|---|
1 | These method never be preceded by static keyword Example: void fun1() { ...... ...... } | These method always preceded by static keyword Example: static void fun2() { ...... ...... } |
2 | Memory is allocated multiple time whenever method is calling. | Memory is allocated only once at the time of loading. |
3 | It is specific to an object so that these are also known as instance method. | These are common to every object so that it is also known as member method or class method. |
4 | These methods always access with object reference Syntax: Objref.methodname(); | These property always access with class reference Syntax: className.methodname(); |
5 | If any method wants to be execute multiple time that can be declare as non static. | If any method wants to be execute only once in the program that can be declare as static . |
What is new keyword ?
A new keyword is used to allocate memory at runtime, new keyword is used for create an object of class
When use volatile keyword ?
If the variable keep on changing such type of variables we have to declare with volatile modifier. Volatile Keyword in Java
Main advantage of volatile keyword ?
The main advantage of Volatile keyword is we can resolve data inconsistency problems.
Main dis-advantage of Volatile ?
The main dis-advantage of Volatile keyword is, crating and maintaining a separate copy for every thread, increases complexity of the programming and effects performance of the system.
Why use synchronized keyword ?
Synchronized Keyword is used for when we want to allowed only one thread at a time then use Synchronized modifier. If a method or block declared as a Synchronized then at a time only one thread is allowed to operate on the given object.
Main advantage of Synchronized keyword ?
The main advantage of Synchronized keyword is we can resolve data inconsistency problem.
The main dis-advantage of Synchronized keyword ?
The main dis-advantage of Synchronized keyword is it increased the waiting time of thread and effect performance of the system, Hence if there is no specific requirement it is never recommended to use synchronized keyword.