
Static Keyword in Java
Static Keyword in Java
- Static keyword is mainly used for memory management.
- Static keyword can be use in variables, methods, blocks and nested classes.
- Static variable values can not be change/modify at any case.
- The main method of a class is generally labelled static.
- To call a static method, we don’t want to create an object, it’s enough to mention the class name alone to access the static methods.
- Static variables/methods can also be called as class variables/class methods.
Example For static variable:
Static stringunvName= “Anna University” ; |
Example For static method:
Public static void main (string arts[]) {……. } |
Example For static block:
Static{ //Set of statements } |
Example For static nested class:
class OuterClass{ static int data=30; static class InnerClass{ void msg(){System.out.println("data is "+data);} } public static void main(String args[]){ OuterClass.InnerClass oi=new OuterClass.InnerClass(); oi.msg(); } } |