What is the role of the static keyword

  static keyword meaning and usage scenarios

  static is one of Java50 keywords. The static keyword can be used to modify the code block for static code block, modify member variables represent global static member variables, modification methods for static methods. (Note: You can not modify the general category, in addition to inner classes, why is this?)

  class A {

  static {

  System.out.println ( "A: static block of code");

  }

  static int i; // static variables

  static void method() {

  System.out.println ( "A: static method");

  }

  }

  In short, the static keyword modified contents are static.

  Static is relative to the dynamic, dynamic refers to the Java program to run on the JVM, JVM dynamically created objects and storing objects (memory allocation) required procedure, after the target mission, the object will be destroyed garbage collector, namely memory recycling JVM unified management and allocated to other newly created object; static means that Java programs are not running, JVM will allocate storage space for the class loaded modified static keyword content; such as static member variables, Java class loading to the JVM, JVM will store static member variables of classes and class methods in area, we know the method area is shared by the threads and the region of GC rare, so is the static keyword modified contents are globally shared, and only it is assigned a storage space.

  So when something does not belong to the class of objects, and when that is shared by the objects belong to the class, you can consider whether modified with the static keyword.

  The role of the static keyword

  1 modified block

  Using a modified static keyword category code block is called static code, and vice versa with no static keyword modified example of code blocks is called code blocks.

  The example code block will be executed with the creation of the object, that is, each object will have its own instance of the code block, the results show it is to run the sample code block will affect the content of the current object, and with the destruction of the object disappear (memory recovery); and a static block when the JVM Java class is loaded into memory and executed code block, since the class loading occurs only during a JVM is running, the static code block is also executed only once.

  Because the primary role static block of code is used for complex initialization, it forms a static class code block stored in the method area to follow is the result of storing static block of code in the method area, i.e. in the process of initializing amount storage area and shared by the thread.

  2 modified member variable

  Class with the static keyword modified member variable called static member variables can not be modified because the static local variable (Why?), So a static member variables can also be referred to as static variables. Static variables with similar code block, the class is loaded into the JVM's memory, static variable into the JVM method will allocate memory area and also shared by the threads. Access the form: name of the class static member name.

  public class StaticTest {

  public static void main(String[] args) {

  System.out.println(D.i);

  System.out.println(new D().i);

  }

  }

  class D {

  static {

  i = 2;

  System.out.println ( "D: static block of code 1");

  }

  static int i;

  }

  operation result:

  D: static block of code 1

  2

  2

  Static variables are stored in the information class and can be shared between threads, of course, it also belongs to the class of each object, so you can access static variables through an object, but the compiler does not support doing so, and gives warnings .

  note:

  Static loading sequence code block class and a static variable of the class. Load Static variable priority class, and then load the static block of code, but a plurality of code blocks and the plurality of static variables will be loaded in the order written.

  class D {

  static {

  i = 2;

  System.out.println ( "D: static block of code 1");

  }

  static {

  i = 6;

  System.out.println ( "D: static block of code 2");

  }

  static int i;

  }

  You can think about operating results.

  Static variables can not explicitly initialized, JVM will default to their respective defaults. As the basic data type of byte is 0, short as 0, char is \ u0000, int is 0, long as 0L, float to 0.0f, double as 0.0d, boolean is false, unified reference type is null.

  Static variable since it is a shared JVM memory and can be changed, then access to it can cause security thread (thread A rewrite while thread B gets its value, the acquisition value is the value before the amendment or modification it?), so while the use of static variables to consider multithreading. If you can make sure that static variables can not be changed, you can use the final keyword together to avoid the use of thread-safety issues; otherwise synchronized manner to avoid the need to use thread-safety issues, such as use with volatile keywords.

  The key can not be modified static local variables, instance methods and static methods, or will the original intention of the static keyword - Share contrary.

  3 modification methods

  With the static keyword modified method called static method, otherwise known as instance methods. The method name invoked by class name, but note that static methods can call other static variables and static methods of a class, can not call member variables and instance methods (unless the call through the object) directly.

  class D {

  static {  Wuxi gynecological examination Which http://www.csjlyyfk.com/

  i = 2;

  System.out.println ( "D: static block of code");

  }

  static final int i;

  int j;

  static void method() {

  System.out.println(i);

  System.out.println(new D().j);

  method1();

  new D().method2();

  }

  static void method1() {

  System.out.println(i);

  }

  void method2() {

  System.out.println(i);

  }

  }

  Note:? Since the lower class instance methods need to call in order to access the object, and static methods can be directly accessed via the class name, then do not consider the deployment server case, a class is how to begin it most likely is the "class name. static method "start Java, which I define as much static method, JVM is how to know the main entrance it?

  Perhaps you think the main method.

  Yes, that is the main method Java specification defines an inlet into a main Java classes. Java classes run by the main ways to open:

  public static void main(String[] args) {

  for (String arg: args) {// Parameters defined by the outer

  System.out.println(arg);

  }

  }

  However, note that Java is not the main keyword, name of the program it is just a method prescribed entrance; another main method can be overloaded.

  Note: static keyword may not modify the general category, but you can use the static keyword modified so that it becomes an inner class static inner classes. static meaning of the keyword itself is shared, and Java classes loaded into the JVM memory area method, is also shared by the threads, so no need to modify the general category with the static keyword.

  4 static imports

  When coated with import or import classes, may be modified static package name or class representing static import. Static import can be placed in comparison with the dynamic import to deepen understanding.

  Dynamic import is when you need to run the program is not a new subject in this package of classes, the class will be based on the full path name to load the class; and static import is loaded with the kind of loaded classes and static import, so it advance imported.

  public class StaticTest {

  static void method1() {

  System.out.println("static method1");

  }

  static void method2() {

  System.out.println("static method2");

  }

  }

  Static import:

  import static com.starry.staticImport.StaticTest.method1;

  public class Client {

  public static void main(String[] args) {

  method1(); //

  StaticTest.method2();

  }

  }

  Note method1 () is a static imports, it can be accessed by class name is not required; and method2 () is not imported, you will need to pass the class name calling. So when you need a static import it?

  The method used to import static and static class, etc. comprising static enumeration method is introduced, or a method of information into the class information may be determined at compile time.

  The disadvantage of the static keyword

  Packaging is one of the three features of Java classes and object-oriented main features. Because no by object classes can be directly access the class attributes and methods, this kind of destruction of class encapsulation; Utils so in addition to the class, code and variables to be modified as little as possible with the method of the static keyword.

Guess you like

Origin www.cnblogs.com/djw12333/p/11314272.html