Java Static keyword most asked Interview questions and answers
(1) What means Static in Java?
Ans :-
Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class.
Static is keyword in java and mainly used for memory management.
Static keyword used in following :-
- Static class
- Static variable
- Static method
- Static Block
Learn more about these follow this link :-
(2) Why main method is static in Java?
Ans :-
Because we can call static method without instantiated, means we does not have to create object of that.
The public static void keyword mean the JVM (Java Virtual Machine) interpreter can call the program's main method to start without creating object of the class, and the program does not return data to the jvm interpreter when it ends.
Read stackoverflow answer for more in depth details.
(3) Can we run static block without main method in Java?
Ans :-
The answer is depends on version of JDK.
Before JDK 7 we can run java code without main() method. means we can run static block without main method.
After JDK 7 main methods is mandatory in java code. when we run our java code then compiler verify first that main method is present or not. if compiler does not found means our code does not contain main method then compiler gives error "main method not found in the class".
So nowadays we all use JDK 8 or above version so answer is
No, static block does not run without main methods or we can say java program does not run without main method.
If you want to learn more:-
(4) Can we define multiple static block in Java?
Ans :-
Yes, in java we can write multiple static block.
If java program contains more than one static block then it runs in same manner as we defined in our program means from top to down.
Refer below example.
(5) Can we declare constructor as static in Java?
Ans :-
No, in java we can not declare constructors as static.
If we try to declare constructor as static we get following error.
If you want to know why we cannot declare constructor as static then follow this link:-
https://beginnersbook.com/2013/05/static-constructor/
(6) Can we Overload Static method in Java?
Ans :-
Overload = Overloading means same method name and return type but different parameter in same class.
Yes, We can overload static method in java. see example below.
(7) Can we Override Static method in Java?
Ans :-
Override = Overriding means same method name and signature but in different class.
We can use same static method name and signature in parent and child class but it not behaves like overriding means We cannot Override static method in java. lets see example.
(8) Can we call Instance method from Static method in Java?
Ans :-
We can not Directly call Instance method from Static method in Java. But The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method.
public class SimpleClass{
public void instanceMethod() {
System.out.println("Instance Method");
}
public static void main(String[] args) {
instanceMethod();
}
}
If we try to call Instance method from Static method like above then it gives compilation error. and suggest to change Instance method to Static method.
We can create Object of class and using class instance we can call Instance method from Static method. Refer below example.
public class SimpleClass{
public void instanceMethod() {
System.out.println("Instance Method");
}
public static void main(String[] args) {
SimpleClass obj = new SimpleClass();
obj.instanceMethod();
}
}
Output :-
Static Method
Learn more about this :-
(9) Can we call Static method from Instance method in Java?
Ans :-
Yes, from Instance method we can call Static method in Java.
public class SimpleClass{
public static void staticMethod() {
System.out.println("Static Method");
}
public void instanceMethod() {
System.out.println("Instance Method");
staticMethod();
}
public static void main(String[] args) {
SimpleClass obj = new SimpleClass();
obj.instanceMethod();
}
}
Output :-
Instance Method
Static Method
(10) Can we declare Abstract method as Static in Java?
Ans :-
No, in java we can not declare abstract methods as static.
(11) Why abstract method not declared as Static in Java?
Ans :-
In java, abstract method does not contains body. means Abstract means subclass must implement abstract method.
Static method can not be override and also static method must need body.
If you want to learn more about it check out stack overflow discussion.
if you want to learn about What is abstraction in java?
if you want to learn about What is Static keyword in java?
(12) Can we use super keyword in Static method in Java?
Ans :-
A static method or, block belongs to the class and these will be loaded into the memory along with the class. You can invoke static methods without creating an object.
Where, the super keyword in Java is used as a reference to the object of the super class. This implies that to use super the method should be invoked by an object, which static methods are not.
Therefore, we cannot use super keyword from static method in Java.
(13) Can we use this keyword in Static method in Java?
Ans :-
The this keyword is used as a reference to an instance. Since the static methods doesn’t have any instance you cannot use the this reference within a static method. If you still, try to do so a compile time error is generated.
(14) Can we declare Static method in Abstract class in Java?
Ans :-
Yes, we can declare Static method in Abstract class. This is allowed because that method can be called directly, even if you do not have an instance of the abstract class.
abstract class AbstarctDemo{
public static void staticMethod() {
System.out.println("Static Method");
}
}
class Demo extends AbstarctDemo{
public static void main(String args[]){
AbstarctDemo.staticMethod();
}
}
Output :-
Static Method
Happy Coding. Happy Learning.
See other interview questions and Answers :-
- Abstract class and Interface Interview Questions and Answers in Java
- Core java interview questions and answers
Comments
Post a Comment