Skip to main content

Top Interview Questions and Answers on Static Keyword | Programming Blog

Java Static keyword most asked Interview questions and answers

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. 

Can we run static block without main method in Java?

(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.

Can we declare constructor as static in Java?

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.

Can we Overload Static method in Java?

(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.

Can we Override Static method in Java?

(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 :-


 

 



Comments

Popular posts from this blog

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st