java static and non-static difference

Static here refers to those modified with the static keyword, including classes, methods, blocks, and fields.

Non-static means that it is not modified with static.

Static has some characteristics:

1. Globally unique, any modification has a global impact

2. Load only once, in preference to non-static

3. The usage does not depend on the instance object.

4. The life cycle belongs to the class level, starting from JVM loading and ending with JVM unloading.

Reference: http://blog.csdn.net/zhandoushi1982/article/details/8453522/.

 

For the difference between static inner classes (nested classes) and non-static inner classes, please refer to:

http://www.jb51.net/article/74838.htm

(1) The inner static class does not need to have a reference to the outer class. But a non-static inner class needs to hold a reference to the outer class.

(2) Non-static inner classes can access static and non-static members of outer classes. A static class cannot access non-static members of the outer class. He can only access static members of the outer class.

(3) A non-static inner class cannot be created without the entity of the outer class. A non-static inner class can access the data and methods of the outer class because it is inside the outer class.

copy code
/* The following program demonstrates how to create static inner classes and non-static inner classes in java */ 
class OuterClass{
   private  static String msg = "GeeksForGeeks" ;
   // static inner class 
  public  static  class NestedStaticClass{
     // static inner class can only be accessed Static member of outer class 
    public  void printMessage() {
      // Try to change msg to non-static, this will result in compilation error 
     System.out.println("Message from nested static class: " + msg);
    }
  }
  // Non-static inner class 
  public  class InnerClass{
     // Regardless of whether it is a static method or a non-static method, you can access 
    public  void display(){ in a non-static inner class
     System.out.println("Message from non-static nested class: "+ msg);
    }
  }
}
class Main
{
  // How to create instances of static inner classes and non-static inner classes 
  public  static  void main(String args[]){
     // Create instances of static inner classes 
    OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();
     // Create static inner classes Class's non-static method 
    printer.printMessage();  
     // In order to create a non-static inner class, we need an instance of the outer class 
    OuterClass outer = new OuterClass();    
    OuterClass.InnerClass inner = outer.new InnerClass ();
     // Call the non-static method 
    inner.display() of the non-static inner class;
     // We can also combine the above steps to create an inner class instance in one step 
    OuterClass.InnerClass innerObject = new OuterClass(). new InnerClass ();
     // Similarly we can now call the inner class method 
    innerObject.display();
  }
}
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325731600&siteId=291194637