Search This Blog

Static in Java

Static is an important keyword of java. We need to understand “static” well in order to make our java programming effective. The runtime system guarantees that static blocks are called in the order that they appear in the source code. They are executed when the JVM loads the class before the object creation Constructor. Below are the few important points about “static”
1) static can be used with method, variables and nested class
Static variable in java belong to each and every object of the class instead of any particular one, which means that static variable will have same value for all the object regardless of what type of variable it is (int ,char, String etc.). So it signifies that there is only one copy of static variable in java heap memory which would be accessed from all the objects.
Static methods are accessible without creation of object of that class. We can use static method using class name only. The best example of static method in java is main method (public static void main(String args[ ])
        2)  We can’t use non-static variable inside a static method or block.
        3)    Static fields gets initialized while class loading in JVM whereas instance variable gets loaded while creation of object.
        4)    We can create static block using static keyword which consist of some code and gets initialized while class loading.
                        Static{
      String message = “Hello World”
      System.out.println(message)
}

          Nested Classses : 
                                      Classes defined inside another class is known as nested class (allowed in java).Nested class can be defined as public,private or protected. There are two types of nested class . 1) static nested class 2) non-static nested class

                           public class OuterClass{
                                          ............
                                       static class NonStaticClass(){
                                                            .....}
                                                 class innerClass(){
                                                            .......}
                                                   }

         A non-static nested class does have access to elements of enclosing class  i.e. OuterClass even if they are marked as private whereas static nested class doesn't have that access.

     Why to use Nested class:
      1) It can be used for logical grouping : If a class is required for only one other class then its logical to create a nested class and group them both together.
      2) It makes code more readable and maintainable: For the case mentioned in previous point, Its easier to maintain and read the code in a single class instead of two different classes.
      3)It increases encapsulation : Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world
   
   


No comments:

Post a Comment