WHAT ARE STATIC VARIABLES? WHY IT IS USED? WHAT IS IT’S USE? – A COMMON INTERVIEW QUESTION.
The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
The static can be:
variable (also known as class variable) method (also known as class method) block nested class
Static variable
For eg:
1
2
3
| public class Test { public static String st_var = "I'm a static variable";} |
1
2
3
4
5
6
7
| public class Application { public static void main(String[] args) { System.out.println(Test.st_var); // call using Class name itself. }}
One common use of static is to create a constant value that’s attached to a class. The only change we need to make to the above example is to add the keyword final in there, to make ‘st_var’ a constant
|

No comments: