Static Keyword in Java

 Static Keyword is a modifier and can be applied to variable, method, static block and static class.

1. Static Variable- only 1 copy, can be access by classname directly, can be used for common thing . 

    For e.g. for n students if college name is same. 
                   Static String collegeName ="IIIT"

2. Static method- can only access static variables and methods. But non static method can access        both static and non static method
    Note- Main method is also a static method. 

3. Static block- Can execute before main method. Static block and static variable are                           executed depends on from top to bottom declaration inside class.

4. Nested Static class- In Java ,we can declare class inside another class. known by names Outer        class and inner class. We can't declare a class as static but we can declare an inner class as             static class. Object declaration is different for accessing inner class.
    Refer- https://www.geeksforgeeks.org/static-class-in-java/

For details of static keyword-Refer- https://www.geeksforgeeks.org/static-keyword-java/

    

   




Comments

Popular posts from this blog

Polymorphism

Thread and Thread Safety in Java.