Static variables used in Java
A member of the static decoration in 1.Java is called a statically member or a class member. It belongs to the entire class, not to an object, which is shared by all objects of the class and takes precedence over the existence of the object. Static members can be accessed directly by using the class name or by using the object name. Use static to modify variables, methods, and blocks of code.
The 2.public modifier denotes public, public, static variables using the Statics modifier
3. Static methods can call static members directly in the same class, but not non-static members directly.
public class hellworld{String name = "Java";//non-static variable static string hobby = "programing";//static variable public static Voi D print () {System.out.println ("Welcome:" + name + "!"); /cannot directly invoke non-static variable System.out.println ("Welcome you:" + Hobby + "!"); /can invoke static variable directly}
}
4. If you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.
public class hellworld{String name = "Java";//non-static variable static String hobby = "program";//static variable//static method call non-static variable Pu Blic static void print () {//Create object of Class HelloWorld hello=new HelloWorld (); The use of objects to implement a static method called non-static variable System.out.println ("Welcome you:" +hello.name+ "!"); Static variables can be directly called SYSTEM.OUT.PRINLTN ("Welcome" + hobby + "+hello.name"). } }
5. In the ordinary member method, you can directly access the same non-static variables and static variables
public class hellworld{String name = "Java";//non-static variable static string hobby = "programing";//static variable public void print () {//Common method System.out.println ("Welcome you:" + name + "!"); SYSTEM.OUT.PRINLTN ("Welcome like" + Hobby + "+hello.name"); } }
6. Non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects
public class hellworld{String name = "Java";//non-static variable static string hobby = "program";//static variable//non-static method public Vo ID Show () {System.out.println ("I am a non-static method and cannot be called directly by a static method ... ”); }//static method public static void Show2 () {System.out.println ("I am a static method, can be called directly by a static method"); }//The non-static method is invoked through an object in a static method. Static methods can be called directly public static void print () {//Create objects of Class HelloWorld hello=new HelloWorld (); The use of objects to implement a static method called non-static method Hello.show (); Static methods can be called directly in the static method Show2 (); } }
Static initialization block used in Java
1. In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when an instance of the class is created. If you initialize a block with the static adornment, it is called a static initialization block.
2. The difference between an instance variable and a class variable:
A) storage location: Class variables are stored in the method area as the class is loaded, and instance variables exist in heap memory as the object is built.
b) Life cycle: The class variable has the longest life cycle and disappears as the class disappears; the instance variable life cycle disappears as the object disappears.
3. Static initialization blocks are executed only once when the class is loaded, and static initialization blocks can only assign values to static variables and cannot initialize normal member variables. When the program runs, static initialization is executed first and takes precedence over the main function, then the normal initialization block is executed, and the construction method is finally executed.
public class Staticdemo {int num1;//declaration variable 1 int num2;//declaration variable 2 static int num3;//declaration static Variable 3 public Staticdemo () {//constructor method Nu m1 = 88; System.out.println ("Assign a value to variable 1 by construction method"); } {//initialization block num2 = 99; System.out.println ("Assign a value to variable 2 by initializing the Block"); static{//static initialization cannot be Giel the normal variable assignment num3 = 77; SYSTEM.OUT.PRINTLN ("Static variable 3 is assigned by static initialization block"); } public static void Main (string[] args) {Staticdemo Hello = new Staticdemo ();//create Object of Class Hello System.out.println ("Nu M1: "+ hello.num1); System.out.println ("num2:" + hello.num2); System.out.println ("num3:" + hello.num3); Staticdemo hello1 = new Staticdemo (); }}
Operation Result:
The use of static in Java