Java static keyword detailed introduction and usage summary

  • 2020-06-23 00:13:52
  • OfStack

static stands for "global" or "static" and is used to modify member variables and methods. It can also form static static code blocks, but there is no concept of global variables in Java. The following arrangement of the content you can refer to.

1. Static methods

Typically, a method in a class is defined as static, that is, the method can be called without an object of this class

The method declared as static has the following restrictions:

They can only call other static methods.

They can only access static data.

They cannot refer to this or super in any way.


class Simple { 
  static void go() { 
   System.out.println("Welcome"); 
  } 
 } 
 public class Cal { 
  public static void main(String[] args) { 
   Simple.go(); 
 } 
 } 

Calling a static method is "class name. Method name." Static methods are simple to use as shown above. In general, static methods often provide utilities for other classes in an application Java Many of the static methods in the class library of The.

2. Static variables

Variables declared as static are essentially global variables. When an object is declared, a copy of the static variable is not produced; instead, all instance variables of the class share the same static variable. Static variables are similar to static methods. All instances of this class share this static variable, meaning that only 1 block of storage is allocated when the class is loaded, and all objects of this class can manipulate this block of storage, except for final


class Value { 
  static int c = 0; 
   static void inc() { 
   c++; 
  } 
 } public class Count2 { 
  public static void prt(String s) { 
   System.out.print(s); 
  } 
 public static void main(String[] args) {   
  Value v1, v2; 
   v1 = new Value(); 
   v2 = new Value(); 
   prt("v1.c=" + v1.c + " v2.c=" + v2.c); 
   v1.inc(); 
   prt(" v1.c=" + v1.c + " v2.c=" + v2.c); 
  } 
 } 

The results are: v1.ES38en =0 v2.ES40en =0 v1.ES42en =1 v2.c =1

This proves that they share 1 block of storage. The static variable is somewhat similar to the concept of global variables in C.

It is worth exploring the initialization of static variables.

If you need to initialize your static variable by calculation, you can declare a block of static, which is executed only once when the class is loaded. The following example shows a class with 1 static method, 1 static variable, and 1 static initialization block:


class Value3 { 
  static int c = 0; 
   Value3() { 
   c = 15; 
  } 
   Value3(int i) { 
   c = i; 
  } 
   static void inc() { 
   c++; 
  } 
 } 
 public class Count { 
  public static void prt(String s) { 
   System.out.println(s); 
  } 
  Value3 v = new Value3(10); 
  static Value3 v1, v2; 
  static {// This is the static block  
   prt("v1.c=" + v1.c + " v2.c=" + v2.c); 
   v1 = new Value3(27); 
   prt("v1.c=" + v1.c + " v2.c=" + v2.c); 
   v2 = new Value3(15); 
   prt("v1.c=" + v1.c + " v2.c=" + v2.c); 
  } 
   public static void main(String[] args) { 
   Count ct = new Count(); 
   prt("ct.c=" + ct.v.c); 
   prt("v1.c=" + v1.c + " v2.c=" + v2.c); 
   v1.inc(); 
   prt("v1.c=" + v1.c + " v2.c=" + v2.c); 
   prt("ct.c=" + ct.v.c); 
  } 
 } 

The result is es62EN1.c =0 ES64en2.ES65en =0

v1.c=27 v2.c=27

v1.c=15 v2.c=15

ct.c=10

v1.c=10 v2.c=10

v1.c=11 v2.c=11

ct.c=11

This program demonstrates various features of static initialization. If you're new to Java, the results may surprise you. You may be confused by the static postbracket. The first thing to tell you is that the variable defined by static takes precedence over any other non-ES89en variable, regardless of the order in which it appears. As shown in the program, while v appears before v1 and v2, it turns out that v1 and v2 are initialized before v. The static{is followed by a piece of code that performs explicit static variable initialization only once, when the class is loaded the first time. If you can read and understand this code, it will help you understand the static keyword. When it comes to inheritance, the parent's static variable is initialized first, then the child's, and so on.

3. Static class

Normally a normal class is not allowed to be declared static, only an inner class is allowed. The inner class declared static can be used as a normal class without having to instantiate an outer class.


public class StaticCls { 
  public static void main(String[] args) { 
   OuterCls.InnerCls oi = new OuterCls.InnerCls(); 
  } 
 } 
 class OuterCls { 
 public static class InnerCls { 
   InnerCls() { 
    System.out.println("InnerCls"); 
   } 
  } 
 } 

Result: InnerCls

4. What are static and final1 used for

static final is used to modify member variables and member methods, which can be simply understood as "global constants"!

For variables, the value given once 1 is immutable and accessible by the class name.

For methods, the representation is not overridden and can be accessed directly by the class name.

5. Added:

static stands for "global" or "static" and is used to modify member variables and member methods. Static static blocks can also be formed, but there is no concept of global variables in the Java language.

The member variables and member methods decorated by static are independent of any object of the class. That is, it does not depend on specific instances of the class and is Shared by all instances of the class. As soon as this class is loaded, Java The virtual machine then finds them in the method area of the runtime data area based on the class name. As a result, static An object can be accessed before any of its objects are created without reference to any object.

with public The modified static member variables and member methods are essentially global variables and global methods. When an object of its class is declared, no copy of the static variable is generated. Instead, all instances of the class share one static variable.

So static, we could have it before private This variable can be used in static code blocks of a class, or in other static member methods of a class (or non-static member methods -- nonsense), but cannot be directly referenced in other classes by class name. In fact, you need to understand that private is access limited, static means you can use it without instantiating it, so it's much easier to understand. The effect of static preceded by other access keywords is the same.

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

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

Class name. Static variable name

A block of code decorated with static represents a static block of code that executes when the Java virtual machine (JVM) loads the class (very useful, hehe).

static variables:

Class member variables can be classified according to whether they are static or not: one is the variable modified by static, called static variable or class variable; The other is variables that are not modified by static, called instance variables.

The difference is:

For static variables there is only 1 copy in memory (to save memory), JVM Memory is allocated only once statically, memory is allocated for static variables during class loading, class names are available directly (convenient), and of course objects can be accessed (but this is not recommended).

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

static method

Static methods can be called directly by the class name, and any instance can also be called, so the this and super keywords cannot be used in static methods, and the instance variables and instance methods of the class to which they belong cannot be directly accessed static Can only access static member variables and member methods of the class to which it belongs. Because instance members are associated with a particular object! This need to understand, want to understand the truth, not memory!!

Because the static method is independent of any instance, the static method must be implemented and cannot be abstract abstract.

This article is intended to be helpful to those in need


Related articles: