Home > Java > javaTutorial > Detailed introduction to Java variable types

Detailed introduction to Java variable types

零下一度
Release: 2017-06-29 09:42:46
Original
1207 people have browsed it
  • Local variables

  • Member variables

  • Class variables

Java local variables

  • Local variables are declared in methods, constructors 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, constructor or statement block in which they are declared;

  • Local variables are allocated on the stack.

  • Local variables have no default value, so after the local variable is declared, it must be initialized before it can be used.

  • Instance variables

    • Class variables are also called static variables. They are declared with the static keyword in the class, but Must be outside method constructors and statement blocks.

    • No matter how many objects a class creates, the class only has one copy of the class variable.

    • Static variables are rarely used except when declared as constants. Constants refer to variables declared as public/private, final and static types. Constants cannot be changed after initialization.

    • Static variables are stored in the static storage area. Often declared as constants, variables are rarely declared using static alone.

    • Static variables are created at the beginning of the program and destroyed at the end of the program.

    • Have similar visibility to instance variables. But in order to be visible to users of the class, most static variables are declared as public types.

    • Default values ​​are similar to instance variables. The default value of numeric variables is 0, the default value of Boolean variables is false, and the default value of reference types is null. The value of a variable can be specified when declaring it 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, the class variable name must use uppercase letters. 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.

    • Instance variables are declared in a class, but outside methods, constructors, and statement blocks;

    • When an object is instantiated After that, the value of each instance variable is determined;

    • 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, constructor or statement block, so that the outside can obtain instance variable information through these methods;

    • Instance variables can be declared before use Or after use;

    • Access modifiers can modify instance variables;

    • Instance variables are for methods, constructors or statement blocks in a class visible. In general, instance variables should be made private. Instance variables can be made visible to subclasses by using access modifiers;

    • 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 type variables is null. The value of a variable can be specified at the time of 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: ObjectReference.VariableName.

    • Instance:

      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;   }
      Copy after login
    • Class variable (static variable)

      Instance:

      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);}}
      Copy after login

The above is the detailed content of Detailed introduction to Java variable types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template