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.
Listed below are 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。 double pi = 3.14159; // 声明了pi。 char x = 'x'; // 变量x的值是字符'x'。
The variable types supported by the Java language are:
- Local variables: Variables in methods of a class.
- Instance variables: variables independent of methods, but without static modification.
- Class variables: variables independent of methods, modified with static.
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, 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 on 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 example age is a local variable. It is defined in the pupAge() method, and its scope is limited to this method.
public class Test{ public void pupAge(){ int age = 0; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); } }
The compilation and running results of the above example are as follows:
Puppy age is: 7
Example 2
In the following example the age variable is not initialized, so an error occurs during compilation.
public class Test{ public void pupAge(){ int age; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); } }
The compilation and running results of the above example are as follows:
Test.java:4:variable number might not have been initialized age = age + 7; ^ 1 error
instance variables
- Instance variables are declared within 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 a class, but must be outside methods, constructors and statement blocks.
- 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 and 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 = "Development "; public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT+"average salary:"+salary); } }
The compilation and running results of the above example are as follows:
Development average salary:1000
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.