java: Variable type

The variable types supported by the Java language are:

  • Class variables: Variables independent of methods, modified with static.
  • Instance variables: variables independent of methods, but without static modification.
  • Local variables: variables in the methods of the class.
public class Variable{
    
    
    static int allClicks=0;    // 类变量
 
    String str="hello world";  // 实例变量
 
    public void method(){
    
    
 
        int i =0;  // 局部变量
 
    }
}

Java local variables

  • Local variables are declared in methods, construction methods, or statement blocks;
  • Local variables are created when methods, constructors, or statement blocks are executed. When they are executed, the variables will be destroyed;
  • Access modifiers cannot be used for local variables;
  • Local variables are only visible in the method, construction method, or statement block that declares it;
  • Local variables are allocated on the stack.
  • Local variables have no default value, so after the local variables are declared, they must be initialized before they can be used.
public class Test{
    
     
   public void pupAge(){
    
    
      int age = 0;
      age = age + 7;
      System.out.println("小狗的年龄是: " + age);
   }
   
   public static void main(String[] args){
    
    
      Test test = new Test();
      test.pupAge();
   }
}

Instance variable

  • Instance variables are declared in a class, but outside of methods, constructors, and statement blocks;
  • When an object is instantiated, the value of each instance variable is determined accordingly;
  • Instance variables are created when the object is created, and destroyed when the object is destroyed;
  • The value of an instance variable should be referenced by at least one method, construction method, or statement block, so that the external can obtain instance variable information through these methods;
  • Instance variables can be declared before or after use;
  • Access modifiers can modify instance variables;
  • Instance variables are visible to methods, constructors, or statement blocks in the class. Under normal circumstances, instance variables should be set as private. By using access modifiers, instance variables can be made visible to subclasses;
  • Instance variables have default values. The default value of numeric variables is 0, the default value of boolean variables is false, and the default value of reference variables is null. The value of the variable can be specified in the declaration or in the constructor;
  • Instance variables can be accessed directly through the variable name. But in static methods and other classes, you should use the fully qualified name: ObejectReference.VariableName.
import java.io.*;
public class Employee{
    
    
   // 这个实例变量对子类可见
   public String name;
   // 私有变量,仅在该类可见
   private double salary;
   //在构造器中对name赋值
   public Employee (String empName){
    
    
      name = empName;
   }
   //设定salary的值
   public void setSalary(double empSal){
    
    
      salary = empSal;
   }  
   // 打印信息
   public void printEmp(){
    
    
      System.out.println("名字 : " + name );
      System.out.println("薪水 : " + salary);
   }
 
   public static void main(String[] args){
    
    
      Employee empOne = new Employee("RUNOOB");
      empOne.setSalary(1000.0);
      empOne.printEmp();
   }
}

Class variable (static variable)

  • Class variables are also called static variables. They are declared in the class with the static keyword, but they must be outside the method.
  • No matter how many objects a class creates, the class only has one copy of the class variables.
  • Static variables are rarely used except for being declared as constants. Static variables refer to variables declared as public/private, final and static types. Static variables cannot be changed after initialization.
  • Static variables are stored in the static storage area. It is often declared as a constant, and static is rarely used alone to declare a variable.
  • Static variables are created when they are accessed for the first time and destroyed at the end of the program.
  • Has similar visibility to instance variables. But in order to be visible to users of the class, most static variables are declared as public types.
  • The default value is similar to the instance variable. The default value of numeric variables is 0, the default value of booleans is false, and the default value of reference types is null. The value of the variable can be specified at the time of declaration or in the constructor. In addition, static variables can also be initialized in static statement blocks.
  • Static variables can be accessed through: ClassName.VariableName.
  • When a class variable is declared as a public static final type, it is generally recommended to use capital letters for the name of the class variable. If the static variable is not of public or final type, its naming method is consistent with the naming method of instance variables and local variables.
import java.io.*;
 
public class Employee {
    
    
    //salary是静态的私有变量
    private static double salary;
    // DEPARTMENT是一个常量
    public static final String DEPARTMENT = "开发人员";
    public static void main(String[] args){
    
    
    salary = 10000;
        System.out.println(DEPARTMENT+"平均工资:"+salary);
    }
}

Note: If other classes want to access the variable, they can access it like this: Employee.DEPARTMENT.

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113484509