Static Keyword Interview Questions in Java
Interview Questions on Static Keyword in Java
.
What is static keyword in java ?
Static keyword in java are used for memory management
Why we use static keyword in java ?
Static keyword is mainly used for memory management.
What is static variable in java
Variables declared with static keyword is known as static variables.
What is static variable in java? ?
Variables declared with static keyword is known as static variables. If we change any static variable value using a particular object then its value changed for all objects means it is common to every object of that class. This type of variable is class level.
What is static method in java ?
Method which is preceded with static keyword is know as static method. JVM will not call these static methods automatically you need to call these static methods from main method or static block or variable initialization. These types of method called by using class name itself no need to create object.
Note: Only Main method will b called by JVM automatically.
Example
static void show() { ...... ...... }
What is static block in Java ?
Static blocks are the blocks which will have braces and with static keyword and these block execute at the time of class loading.
Example
static { ...... ...... }
What is the need of static block ?
Static blocks will be executed at the time of class loading. If you want to execute any logic before main method loading you should go with static block.
Why main method is static in java ?
To execute main method without creating object.
.Can we overload static methods in java ?
Yes
Can we override static methods in java ?
No
Can we write static public void main(String [] args) ?
Yes, Order of modifiers can change.