1/17/15

static in Java

Static in Java


I am describing how static is special and very useful in java -

  1. "static" is a keyword in Java.
  2. It has a special memory allocated called "Class Area" memory. So static goes into that memory.
  3. It is not a part of object. static elements are shared among all the objects of that class.
  4. Static Class : You can not make a class static.
  5. Static Method : you can have static method. These methods can be invoked without making object of that class, it will also depend upon accessibility of that class.
  6. Static Block : you can have static blocks inside your class. Remember this , these blocks run before constructor. And they can also run without constructor.                                                      
  7. For Example -  If you load JDBC driver to use a connection with class loader. And in next line you are allowed to use DriverManager.getConnection  now this getconnection method is static and that is why you can use it only by loading the class. This shows that java uses static in a lot of ways and it is important.
  8.  Static Variable : when a variable is static it can be used without making object of that class.
  9. Static variables are called class variables : because they are not part of object, they are part of class only.
  10. Most of the variables and methods are static in java Math package.
  11. You can make a Inner class as static.
  12. Generally we make our utility methods static, if we want to use that method again and again in various classes and we do not want to make object every time.
Static in Java is very common and widely used.