- Static is a keyword in java.
- The main use of static keyword is to save memory.
- To get available at class level.
- 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."

- 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.
- 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.
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.
- 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;7. Can static variables be accessible in non-static method?
public void setStaticVariable(int counter) {
count = counter;
}
- Yes, static variable can be accessible in non-static methods. Because as the static variables are loaded during class load.
- No, since the non-static variables are not available during class load.

- Yes, we can overload static methods in java.
- No, we can not override static methods 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.
- Yes, we can have multiple static blocks inside a class.
- The static blocks will be executed in their order written inside class.
- 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"
- No, since abstract methods does not have body and they must have to override in child class whereas static methods can not be overridden.
- 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.
- 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".
If I missed anything or need any improvement in the post please comment below. I will update the same.
All the very best!!