English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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 the Java data type. identifier is the variable name. Multiple variables of the same type can be declared using commas.
The following lists some examples of variable declarations. Note that some include initialization processes.
int a, b, c; // Declare three int type integers: a, b, c int d = 3, e = 4, f = 5; // Declare three integers and assign initial values byte z = 22; // Declare and initialize z String s = "w3codebox"; // Declare and initialize the string s double pi = 3.14159; // Declared a double-precision floating-point variable pi char x = 'x'; // Declare the value of variable x as the character 'x'.
Variable types supported by the Java language include:
Class variable: variables independent of methods, modified with static.
Example variable: variables independent of methods, but without the static modifier.
Local variable: variables within a class method.
public class Variable{ static int allClicks=0; // Class variable String str="hello world"; // Instance variables public void method(){ int i = 0; // Local variables } }
Local variables are declared within methods, constructors, or blocks;
Local variables are created when the method, constructor, or block is executed, and they are destroyed when the execution is completed;
Access modifiers cannot be used for local variables;
Local variables are only visible within the method, constructor, or block in which they are declared;
Local variables are allocated on the stack.
Local variables do not have default values, so local variables must be initialized after they are declared before they can be used.
In the following example, age is a local variable. Defined within the pupAge() method, its scope is limited to this method.
package com.w3codebox.test; public class Test{ public void pupAge(){ int age = 0; age = age + 7; System.out.println("The age of the puppy is: ") + 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:
The age of the puppy is: 7
In the following example, the age variable has not been initialized, so an error will occur during compilation:
package com.w3codebox.test; public class Test{ public void pupAge(){ int age; age = age + 7; System.out.println("The age of the puppy is: ") + 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
Example variables are declared within a class, but outside of methods, constructors, or blocks;
After an object is instantiated, the values of each example variable are determined;
Example variables are created when an object is created and destroyed when the object is destroyed;
The value of an example variable should be referenced by at least one method, constructor, or block, so that external access to the variable information can be achieved through these means;
Example variables can be declared before or after their use;
Access modifiers can be used to modify example variables;
Example variables are visible within the methods, constructors, or blocks of a class. Generally, example variables should be set to private. Access modifiers can be used to make example variables visible to subclasses;
Example variables have default values. The default value for numeric variables is 0, for boolean variables is false, and for reference type variables is null. The value of a variable can be specified at the time of declaration, or in the constructor;
Example variables can be accessed directly by their names. However, in static methods and other classes, it is necessary to use the fully qualified name: ObejectReference.VariableName.
import java.io.*; public class Employee{ // This instance variable is visible to subclasses public String name; // Private variable, visible only within this class private double salary; //Assign name in the constructor public Employee (String empName){ name = empName; } //Set the value of salary public void setSalary(double empSal){ salary = empSal; } // Print information public void printEmp(){ System.out.println("Name : " + name ); System.out.println("Salary : " + salary); } public static void main(String[] args){ Employee empOne = new Employee("w3codebox"); empOne.setSalary(1000.0); empOne.printEmp(); } }
The compilation and running results of the above examples are as follows:
$ javac Employee.java $ java Employee Name : w3codebox Salary : 1000.0
Class variables, also known as static variables, are declared with the static keyword within the class, but must be outside of any method.
Regardless of how many objects a class creates, the class only has one copy of the class variable.
Static variables are rarely used except as constants. Constants are declared as public/Variables of private, final, and static types. Constants, once initialized, cannot be changed.
Static variables are stored in the static storage area. They are often declared as constants and are rarely used alone with the static keyword.
Static variables are created when they are first accessed and destroyed when the program ends.
has a similar visibility to example variables. However, most static variables are declared as public to make them visible to the users of the class.
The default value is similar to example variables. The default value for numeric variables is 0, for boolean variables is false, and for reference types is null. The value of a variable can be specified at the time of declaration, or in the constructor. In addition, static variables can also be initialized in a static block.
Static variables can be accessed via:ClassName.VariableNameaccessed in the following manner.
Class variables declared as public static final type generally suggest using uppercase letters for the variable name. If the static variable is not of public and final type, its naming convention is consistent with that of example variables and local variables.
Example:
import java.io.*; public class Employee { //salary is a static private variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Developer"; public static void main(String[] args){ salary = 10000; System.out.println(DEPARTMENT+"Average Salary:"+salary); } }
The compilation and running results of the above examples are as follows:
Average Salary for Developers:10000.0
Note:If other classes want to access this variable, they can do so as follows:Employee.DEPARTMENT.
In this chapter, we learned about Java variable types. In the next chapter, we will introduce the use of Java modifiers.