The following is referenced from Http://wiki.jikexueyuan.com/project/java/variable-types.html:
A variable provides a named store that the program can Manipulate. each variable in Java has a specific type, which determines the size and layout of the variable memory; the range of values that can be stored in the memory; and a set of actions that can be applied to a variable.
You must now declare the variables you want to use before you use Them. The basic format for declaring variables is as Follows:
Data type variable [= value][, variable [= value] ...];
The data type here is a datatype of java, and variable is the name of a variable. To declare more than one specific variable type, you can separate it with Commas.
The following are examples of valid variable declarations and assignments in Java:
int // declares three ints, a, b, and C. int // Example of initialization byte // Initializes a byte type variable B. Double // declares and assigns a value of PI. Char // the char variable A IIS initialized with value ' a '
There are three types of variables in Java:
- Local Variables (local variables)
- Instance variable
- class, static variable
first, Local Variables (local variables)
- Local variables are declared in a method, constructor, or block
- Local variables are created when a method, constructor, or block is entered, and once you exit the variable it will be Destroyed.
- An accessible descriptor cannot be used on a local variable
- Local variables are visible only in declared methods, constructors, or blocks
- Local variables are implemented inside the stack depth
- The local variable has no default value , so the local variable must be declared and assigned a value before the first use
Example:
here, age is the local variable. This is defined under the Pupage () method, which is scoped to this method only.
public class test{ publicvoid pupage () { int = 0; = age + 7; System.out.println ("Puppy:" + age ); } public Static void main (String args[]) { new Test (); Test.pupage (); }} // the above code will output the following results: Puppy Age Is:7
Example:
The following example uses the local variable age but does not initialize , so the edit will display an error .
public classtest{ public voidpupage () {intage ; age= Age + 7; System.out.println ("Puppy Age is:" +age ); } public Static voidmain (String args[]) {test test=NewTest (); Test.pupage (); }}//the following error is generated when Editing:Test.java:4: Variable number might not has been initializedage= Age + 7;^1 Error
second, the instance variable
- An instance variable is declared in a class, but it is outside the method, constructor, or Block.
- When an object in the heap is allocated a space, the location of each instance variable is Created.
- When the object is created with the keyword "new", The instance variable is created, and it is destroyed when the object is Destroyed.
- The value of an instance variable must be referenced by more than one method, constructor, or block, or an important part of the state of the object that must appear in the class.
- Instance variables can be declared at the class level, before use or after Use.
- An instance variable can be accessed with an accessible descriptor.
- Instance variables are visible to all methods, constructors, or blocks in a class. In general, these variables are recommended to be private (access level). however, the visibility of subclasses can be given to those variables by accessible Descriptors.
- The instance variable has a default value . The default value for the number is zero, the default value for the Boolean is false, and the default value for the object reference is Null. Assignments can be made within a declaration or Constructor.
- Instance variables can be accessed directly in the class by calling the Name. however, the fully qualified name should be used in static methods and in different classes (instance variables can be accessed). Objectreference.variablename
Example:
ImportJava.io.*; public classemployee{//This instance variable is the visible for all child class. publicString name; //salary variable is the visible in Employee class Only. Private Doublesalary; //the name variable is assigned in the Constructor. publicEmployee (String Empname) {name=empname; } //the salary variable is assigned a Value. public voidSetsalary (DoubleEmpsal) {salary=empsal; } //this method Prints the employee Details. public voidprintemp () {System.out.println ("name:" +name); System.out.println ("salary:" +salary); } public Static voidmain (String args[]) {Employee empone=NewEmployee ("ransika"); Empone.setsalary (1000); Empone.printemp (); }}//the above code will output the following results:name:ransikasalary:1000.0
third, class, Static variables
- A class variable is also called a static variable, which is declared in a class with the static keyword, but it is outside the method, constructor, or Block.
- There is only one class variable in each class, no matter how many objects the class Has.
- In addition to being declared as a constant, class variables are rarely applied. Constants are variables that are declared as public, private, final, and Static. The value of the instance initialization is not Changed.
- Static variables are stored in static memory, and in addition to declaring final, static variables rarely use public or rpivate as Constants.
- Static variables begin and end as the program starts and ENDS.
- Visibility and instance variables are similar. however, Most static variables are declared public because they must be used by the consumer of the class.
- The default value is similar to the instance Variable. The default value for a number is zero, the Boolean default is false, and the default value for the object reference is Null. Assignments can be made within a declaration or Constructor. In addition, you can assign a value in a special static initialization Area.
- Static variables can be accessed with the name of the class. Classname.variablename
- When a static variable is declared as public static final, the variable (constant) name is Capitalized. If the static variable is not public and final, its naming method and instance variable are the same as the local Variable.
- cannot be assigned a value after it is declared Final.
Example:
ImportJava.io.*; public classemployee{//salary variable is a private static variable Private Static Doublesalary; //DEPARTMENT is a constant public Static FinalString DEPARTMENT = "development"; public Static voidmain (String Args[]) {salary= 1000; System.out.println (DEPARTMENT+ "average salary:" +salary); }}//the above code will output the following results:Development average salary:1000
Note: If a variable is accessed from outside the class, the constant must be accessed with Employee.department.
Test Project: Https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test3
Types of variables in Java