1. Static variables used in static in Java
1. Members in Java that are modified by static are called static members or class members. It belongs to the entire class, not to an object, that is, it is shared by all objects of the class and exists in priority over objects. Static members can be accessed directly using class names or object names. Use static to modify variables, methods, and code blocks.
2. The public modifier represents public and public, and static variables are modified with static variables.
3. In a static method, static members in the same class can be called directly, but non-static members cannot be called directly.
public class HellWorld{ String name = "Java"; //Non-static variable static String hobby = "programming"; //static variable public static void print(){ System.out.println("Welcome: " + name + " !"); //Non-static variable System.out.println("Welcome: " + hobby + " !"); //Can directly call static variables} }4. If you want to call non-static variables in a static method, you can create an object of the class and then access the non-static variables through the object.
public class HelloWorld{ String name = "Java";//Non-static variable static String hobby = "program";//static variable//Calling non-static variable public static void print(){ //Creating the object of the class HelloWorld hello=new HelloWorld(); //Use objects to call the non-static variable System.out.println in a static method ("Welcome: "+hello.name+"!"); //In a static method, you can directly call the static variable System.out.printn("Welcome to like" +program+""+hello.name); } }5. In ordinary member methods, you can directly access the same type of non-static variables and static variables.
public class HellWorld{ String name = "Java"; //Non-static variable static String hobby = "programing"; //static variable public void print(){//Normal method System.out.println("Welcome:" + name + " !"); System.out.prinltn("Welcome to like" + program+"+hello.name); } }6. Non-static methods cannot be called directly in static methods. 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 void show(){ System.out.println("I am a non-static method, and cannot be called directly by static method...."); } //Static method public static void show2(){ System.out.println("I am a static method, and can be called directly by static method"); } //Non-static method is called through objects in static methods. You can directly call the static method public static void print(){ //Create the object of the class HelloWorld hello=new HelloWorld(); //Calculate the non-static method hello.show() in the static method; //Calculate the static method show2(); } }2. Static initialization blocks used in Java
1. In the class declaration, multiple initialization blocks can be included. When an instance of the class is created, these blocks of code will be executed in turn. If you use static to modify the initialization block, it is called a static initialization block.
2. The difference between instance variables and class variables:
a) Storage location: Class variables are stored in the method area as the class is loaded; instance variables exist in heap memory as the object is established.
b) Life cycle: The life cycle of a class variable is the longest and disappears as the class disappears; the life cycle of an instance variable disappears as the object disappears.
3. The static initialization block is only executed when the class is loaded and will only be executed once. At the same time, the static initialization block can only assign values to static variables and cannot initialize ordinary member variables. When the program is running, the static initialization fast is first executed and takes precedence over the main function, then the normal initialization block is executed, and finally the construction method is executed.
public class StaticDemo { int num1;//Declare variable 1 int num2;//Declare variable 2 static int num3;//Declare static variable 3 public StaticDemo(){//Constructor method num1 = 88; System.out.println("Assign value to variable 1 through construction method"); } {//Initialization block num2 = 99; System.out.println("Assign value to variable 2 through initialization block"); } static{//Static initialization Here you cannot giel normal variables assign value num3 = 77; System.out.println("Assign value to static variable 3 through static initialization block"); } public static void main(String[] args) { StaticDemo hello = new StaticDemo();//Create the object of the class hello System.out.println("num1:" + hello.num1); System.out.println("num2:" + hello.num2); System.out.println("num3:" + hello.num3); StaticDemo hello1 = new StaticDemo(); }} Running results: