Java variable types
In the Java language, all variables must be declared before use. The basic format for declaring variables is as follows:
type identifier [ = value][, identifier [= value] ...] ;
Format description: type is a Java data type. identifier is the variable name. Multiple variables of the same type can be declared separated by commas.
The following lists some examples of variable declarations. Note that some include an initialization process.
int a, b, c; // 声明三个int型整数:a、 b、c int d = 3, e, f = 5; // 声明三个整数并赋予初值 byte z = 22; // 声明并初始化 z String s = "php" // 声明并初始化字符串 s double pi = 3.14159; // 声明了双精度浮点型变量 pi char x = 'x'; // 声明变量 x 的值是字符 'x'。
The variable types supported by Java language are:
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 a local variable is declared, it must be initialized before it can be used.
Example 1
In the following examples age is a local variable. It is defined in the pubAge() method, and its scope is limited to this method.
package com.php.test; 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(); } }
The above example compilation and running results are as follows:
小狗的年龄是: 7
Example 2
In the following example, the age variable is not initialized, so an error will occur during compilation:
public class Test{ public void pupAge(){ int age; age = age + 7; System.out.println("小狗的年龄是 : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); } }
The compilation and running results of the above examples are as follows:
Test.java:4:variable number might not have been initialized age = age + 7; ^ 1 error
Instance variables
- ## Instance variables are declared in a class, but outside methods, constructors and statement blocks;
- When an object is instantiated, 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 or after use;
Access modifiers can modify instance variables;
Instance variables are visible to methods, constructors, or statement blocks in a class. 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.
Example:
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 : " + name ); System.out.println("salary :" + salary); } public static void main(String args[]){ Employee empOne = new Employee("Ransika"); empOne.setSalary(1000); empOne.printEmp(); } }
The compilation and running results of the above example are as follows:
name : Ransika salary :1000.0
Class variables (static variables)
Class variables are also called static variables and are declared with the static keyword in the class, but must be outside the method constructor and statement block.
No matter how many objects a class creates, the class only has one copy of the class variables.
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 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.
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.
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.
Example:
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); } }
The compilation and running results of the above example are as follows:
开发人员平均工资:10000.0
Note: If other classes want to access this variable, they can access it like this: Employee.DEPARTMENT.
In this chapter we learned about Java variable types, and in the next chapter we will introduce the use of Java modifiers.