Usage examples of static methods in JAVA

  • 2020-04-01 04:28:18
  • OfStack

This article illustrates the use of static methods in JAVA. Share with you for your reference, as follows:

Static means "global" or "static". It is used to modify member variables and methods, and can also form static static blocks of code, but there is no concept of global variables in the Java language.

Member variables and methods that are treated as static are independent of any object in the class. That is, it is not dependent on a class-specific instance and is Shared by all instances of the class. As soon as the class is loaded, the Java virtual machine can find it in the runtime data area or method area based on the class name. Therefore, a static object can be accessed before any of its objects are created without reference to any object.

The static member variable and member method modified by public are global variables and global methods in essence. When declaring the object of its class, a copy of the static variable is not generated, but all instances of the class share the same static variable.

The static variable can be preceded by a private modifier, which means that the variable can be used in a static block of code of a class, or in other static member methods of the class (it can also be used in non-static member methods), but it is important that it is not directly referenced by the class name in other classes. In fact, you need to understand that private is an access limit, and static means you can use it without instantiating it, which makes it a lot easier to understand. Static with other access keys before the effect of the same analogy.

The member variables and methods of static modification are customarily called static variables and static methods. They can be accessed directly by the class name. The access syntax is:

Class name. Static method name (parameter list...)

Class name. Static variable name

A static block of code is a block of static code that is executed when the Java virtual machine (JVM) loads a class.

Static variables

Class member variables can be classified into two types according to whether they are static or not. The other is a variable that is not static, called an instance variable. The difference between the two is:

For static variables with only one copy in memory (to save memory), the JVM only allocates memory statically once. The allocation of memory for static variables is done during the loading of the class, which can be accessed directly by the class name (convenient), or of course by object (which is not recommended).

For instance variables, memory is allocated for each instance created, and instance variables can have multiple copies in memory without affecting each other (flexible).

A static method

Static methods can be invoked directly by the name of the class, any instance of also can be called, so can't use this in a static method and the super keyword, cannot directly access the class's instance variables and instance methods (that is, not with members of a static member variables and methods), can only access static members of a class of affiliation member variables and methods. Because instance members are associated with specific objects!

Since a static method is independent of any instance, a static method must be implemented, not abstract abstract.

The static block

Static block of code is also called static blocks of code, it is independent of the members of the class in the class of the static block, there can be multiple, position can be literally put, it is not in any way in the body, the JVM will perform these static loading class code block, if there are multiple static code block, the JVM will order in which they appear in the class of execute them in sequence, each block of code will be executed only once. Such as:


package staticPackage;
public class testStatic {
  private static int a;//A static variable
  private int b;//The instance variables
  static {
    testStatic.a = 3;
    System.out.println(a);
    testStatic t = new testStatic();
    t.f();
    t.b = 1000;
    System.out.println(t.b);
  }
  static {
    testStatic.a = 4;
    System.out.println(a);
  }
  public static void main(String[] args) {
    //TODO automatically generates method stubs
  }
  static {
    testStatic.a = 5;
    System.out.println(a);
  }
  public void f() {
    System.out.println("method f() executing...");
  }
  
}

Static blocks of code can be used to assign values to static variables. Taking a last look at these examples, there is a static main method, which the JVM can call directly when it runs the main method without creating an instance. At this point, we should read the article "(link: #)" to understand it.

I hope this article has been helpful to you in Java programming.


Related articles: