Top interview questions on Static keyword in java?

1. What is static in java? What is the use of the static in java? OR what is the importance of static in java?
  • Static is a keyword in java.
  • The main use of static keyword is to save memory.
  • To get available at class level.
2. Can we declare variable as static? If yes, how to declare static variable?
  • Yes, we can declare a variable as static variable.
  • different ways to declaring static variables
    • declare a static variable without defining.
      • static int count;
    • declare static variable with definition.
      • static int count=5;
  • A local can not be static, since local variables are available within method body.
  • When we try to declare a local variable as static then we will get "illegal modifier for parameter; only final is permitted on local variables."
3. What is the use of static variable?
  • Static variables are used for saving memory allocation. It means that when we declare a variable as static, memory is allocated only once irrespective of number of instances(objects) created.
  • A static variable will  be shared among all the objects. So we can maintain a common value for all objects.
  • The simple example can be, if we want to count number of objects created. Simply use the below program to count.
4. Can we declare method as static? If yes, how to declare a static method?
  • Yes, we can declare static methods.
  • Syntax for declaring a method as static method:
    • static void staticMethod() {
                    }

  • Static methods can be accessed directly wihtout creating object for class. 
          <<ClassName>>.<<MethodName>>

5. What is the importance of static method in java? OR What is the use of static method?
  • The static method can be used for initializing static variables.
  • When there is a requirement to perform some operation without creating an object.
  • When we want to some common operation irrespective of objects that we create.
6. What are different ways to define static variables in java?
  • Static variables can be defined while declaring them. Syntax → private static int count=2;
  • Static variables can be defined using static blocks. Syntax → 
private static int count;
static {
     count =5;

  • Static variables can be defined using static methods. Syntax → 
private static int count;
public void setStaticVariable(int counter) {
        count = counter;
}
7. Can static variables be accessible in non-static method?

  • Yes, static variable can be accessible in non-static methods. Because as the static variables are loaded during class load.
8. Can non-static variables be accessible in static method?
  • No, since the non-static variables are not available during class load.



9. Can we overload static methods in java?
  • Yes, we can overload static methods in java.
10. Can we override static methods in java?
  • No, we can not override static methods in java.
11. What is static block in java? What is the importance of static block in java?
  • Static block is a block with static keyword and open and close curly braces.
  • Static block runs only once during class load and it doe not have return type.
  • It will not accept any return type. And also it will not accept any object specific keywords i.e., this, super.
  • Inside static block we can define static variables.
  • we can call static methods inside static block.
  • As static blocks execute before main method, we can call static methods from static block to execute them prior to main method.
12. Can we have multiple static blocks inside a class in java?
  • Yes, we can have multiple static blocks inside a class.
  • The static blocks will be executed in their order written inside class.
13. Can constructor be static in java?
  • No, since constructor is used for creating objects.
  • It's no sense to be constructor as static.
  • When we try to make constructor as static then we will get compile time error "Illegal modifier for the constructor in type StaticDemo; only public, protected & private are permitted"
14. Can abstract method be static in java?

  •  No, since abstract methods does not have body and they must have to override in child class whereas static methods can not be overridden.
15. Can interface or abstract class have static variables?

  • Yes, interface or abstract class can have static variables.
  • In case of static variable in interface, it must be defined.
  • In case of static variable in abstract class, it may or may not defined.
16. Can class be static in java?

  • No, if we try to make class as static then we will get compile time error "Illegal modifier for the class Demo; only public, abstract & final are permitted".
Like this there can be n number of questions on static keyword in java. I will update post with more questions whenever I find new and tricky.

If I missed anything or need any improvement in the post please comment below. I will update the same.
All the very best!!
Powered by Blogger.