What is static class, variable, method and field example in Java

What is static in Java?
Static in Java is related to class, if a field is static means it belongs to class, similarly static method belongs to classes and you can access both static method and field using class name e.g. if count field is static in Counter class than you can access it as Counter.count, of course subject to restriction applied by access modifier e.g. private fields are only accessible in class on which they are declared, protected fields are accessible to all classes in same package but only accessible in sub class outside package. See private vs protected vs public for complete details on access modifier. Similarly if you have a static method increment() in Counter class which increment counter than you can call it as Counter.increment(). There is no need to create an Object of Counter class to access either static method or static field, this is main difference between static and non-static members.
Another key thing to note about static members e.g. static variable and static method is that they belong to class instead of object, i.e. value of static variable would be same for all object of Counter class. In this Java tutorial we will learn what is static keyword in Java, What is static variable and static method
and some important features of static in Java, which will not only help to understand key programming concept in Java but also helps in various Java questions up to 2 to 4 years’ experience.
Static keyword - Important points
1. You cannot access non static member inside static context e.g. static method or static block. Following code will result in compile time error in Java


public class Counter {
   private int count;
   public static void main(String args[]){
        System.out.println(count); //compile time error
   }
}



This is one of the most common mistakes made by Java programmer especially one who just started programming in Java. Since main method in Java is static and count variable is non-static in this case, print statement inside main method will throw compile time error.

2. Unlike local variables, Static variables and methods are not thread-safe in Java. They are often cause of various thread-safety issues during concurrent programming in Java. If you are using static variables then you need to properly synchronize its access to avoid thread-safety issues including race conditions.

3. Static methods has advantage in terms of usability as you don't need to create an object while accessing those methods, That's why they are best suited for utility classes and methods, java.lang.Math class is a perfect example where almost all methods are static, subsequently utility classes are also declared final in Java.

4. Another important point about static method is that, you cannot override static method in Java. If you declare same method in sub class i.e. static method with same name and method signature it will hide super class method. This is also referred as method hiding.

5. Static methods are bonded during compile time using static binding, opposite to non-static methods which are bonded during run-time based upon object. Which means run-time polymorphism doesn't apply on static methods in Java.

6. You can also make a class static in Java, except top level classes. Those classes are referred as nested static class in Java. Nested static class is particularly useful to provide improved cohesion, one examples of nested static class is HashMap.Entry, which represent data structure, used to hold HashMap entries.

7. Static keyword can be used to declare static block which is executed during class loading. This is also known as static initializer block in Java, Though beware any exception thrown from static initializer block may result in NoClassDefFoundError in Java.

8. One of the important properties of Static field is initialization. Static fields and variables are initialized when class is loaded into memory. They are initialized from top to bottom. Since static fields are initialized in thread-safe manner, this property is also used to implement Singleton pattern in Java and if you are not using Enum as Singleton due to whatever reason, this is a good alternative.

9. During Serialization, like transient variables, static variables are also not serialized.

10. Another feature which is introduced in Java 5 is called static import, which allows to import one or all static members from a class.

That's all on what is Static in Java for now. We have learned about static variables or static fields, static method, static initializer block and static import in Java. We have also learned some important properties of static in Java, which is critical to write and understand Java program. I suggest to get yourself familiar with static in Java, it’s absolutely important for serious programming in Java.


Post a Comment