1. What is static keyword in Java?
Static is a Non-Access Modifier. Static can be applied to variable, method, nested class and initialization blocks (static block).
2. What is a static variable?
- A Static variable gets memory allocated only once during the time of class loading.
- All the instance of the class share the same copy of the variable, a static variable can be accessed directly by calling “<<ClassName>>.<<VariableName>>” without need to create an instance for the class.
- value of a static variable will be common for all instances
public class StaticVariableExample { static int a =10; public static void main(String args[]){ StaticVariableExample s1 = new StaticVariableExample(); StaticVariableExample s2 = new StaticVariableExample(); System.out.println("s1.a value :"+s1.a); System.out.println("s2.a value :"+s2.a); //Change s1 a value alone s1.a=20; System.out.println("s1.a value :"+s1.a); System.out.println("s2.a value :"+s2.a); } }
Output will be
s1.a value :10
s2.a value :10
s1.a value :20
s2.a value :20
- Local variables cannot be assigned as static it will throw compile time error “illegal start of expression”, as the memory cannot be assigned during class load.
3. What is a static method?
- A static method belongs to the class rather than an object. It can be called directly by using the class name “<<ClassName>>.<<MethodName>>”
- A static method can access static variables directly and it cannot access non-static variables and can only call a static method directly and it cannot call a non-static method from it.
- Only the main() method which is static will be called by the JVM automatically, Not all the static method will be called automatically.
4. Can a static block exist without a main() method?
No. You cannot have a static block alone in the class without a main method.
This behavior was a valid one in Java 6, If you have added a System.exit(0) at the end of the static-block, it will run with no errors without a valid main () method. This is because the static block is executed before a valid main method
However, this behavior was changed from Java 7 onwards, now you must include an explicit main () method, even though if it is never reached. For more details JLS chapter 12.4
5. Can we Overload static methods in Java
Yes, you can overload a static method in Java. Read More…
6. Can we Override static methods in Java
No, you cannot override a static method in Java as there will not be any Run-time Polymorphism happening. Read More…
7. Why main() method is declared as static?
If our main() method is not declared as static then the JVM has to create an object first and call which causes the problem of having extra memory allocation.
8. What is a static block?
- A static block is a block of code inside a Java class that will be executed when a class is first loaded into the JVM. Mostly the static block will be used for initializing the variables.
- The static block will be called only one while loading and it cannot have any return type, or any keywords (this or super).
class test { static int val; static { val = 100; } }
9. Can we have multiple static blocks in our code?
Yes, we can have more than one static block in our code. It will be executed in the same order it is written.
10. What is a static class?
- In Java only nested classes are allowed to be declared as static, a top level class cannot be declared as static.
- Even though static classes are nested inside a class, they doesn’t need the reference of the outer class they act like outer class only. Read More…
11. Can constructors be static in Java?
In general, a static method means that “The Method belongs to the class and not to any particular object” but a constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.
12. Why abstract method cannot be static in Java?
Suppose when you have a concrete method in an abstract class then that method can be static. Suppose we have a class like below
public class AbstractTest { static void disp() { System.out.println("disp of static method"); } }
Then the disp() can be accessed by “AbstractTest.disp()”
However, for the same reason cannot be applied when you declare a static method to be abstract. Since static method can be called directly, making it abstract would make it possible to call an undefined method which is of no use, hence it is not allowed.
13. Can Interface in Java have static methods in it ?
Yes, From Java 8 onwards the interface can have static methods in it.
Before Java 8, Interface cannot have static methods in it because all methods are implicitly abstract. This is why an interface cannot have a static method.
14. Can abstract class have static variable in it ?
Yes, an abstract class can have static variables in it.
15. non-static method cannot be referenced from a static context ?
public class Test { /** Non Static main method with String[] args**/ public static void main(String[] args) { welcome(); } void welcome() { System.out.println("Welcom to JavaInterviewPoint"); } }
The welcome() method which we tried calling is an instance-level method, we do not have an instance to call it . static methods belong to the class, non-static methods belong to instances of the class and hence it throws the error ” non-static method cannot be referenced from a static context “.
Priya says
All the above question are really very helpful for any java interview. I am searching for such sites , very well explained , thanks for sharing.
Kamlesh Chouhan says
good question for fresher as well experience.
Mohit thakor says
Thank you so much sir for such type of information….very helpful need more..
Damian says
Questions number 8.
Nonstatic variable can not accessible inside static block.
Correct me if I wrong here.
javainterviewpoint says
Yes, you are correct. I have corrected it now, thank you for the valuable feedback
Sushma says
Hi,
Question no 4
As per java 8 ,static method cannot exist without main().
Please correct if I am wrong.
javainterviewpoint says
Yep, it was true from JDK 7 onwards, but while writing this article (JDK 6) this was a valid scenario. Thank you for the catch.