Java variable type

Java variable type

In the Java language, all variables must be declared before they are used. 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. You can use commas to declare multiple variables of the same type.
Here are some examples of declarations of variables. Note that some contain the initialization process.
Int a , b , c ;         // declare three int integers: a, b, c Int d = 3 , e = 4 , f = 5 ; // declare three integers and give the initial value Byte z = 22 ;         // declare and initialize z String s = " runoob " ; // Declare and initialize the string s Double pi = . 3 .14159 ; // declares double precision floating point variable pi Char x = ' x ' ;        // The value of the declared variable x is the character 'x'.
The Java language supports variable types:
  • Local variable
  • Member variables
  • Class variable

Java local variables

  • Local variables are declared in methods, constructors, or statements;
  • Local variables are created when methods, constructors, or statements are executed, and variables are destroyed when they are executed;
  • Access modifiers can not be used for local variables;
  • Local variables are only visible in the method, constructor, or statement block that declares it;
  • Local variables are allocated on the stack.
  • Local variables do not have default values, so local variables are declared and must be initialized before they can be used.

Example 1

In the following example, age is a local variable. Defined in the pupAge () method, its scope is limited to this method.
Package COM . runoob . Test ; public class the Test { public void pupAge ( ) { int Age = 0 ; Age = Age + . 7 ; the System . OUT . the println ( " dog's age: " + Age ) ; } Public static void main ( String args [ ] ) { the Test Test = new new the Test ( ) ; Test . PupAge ( ) ; } }
The above example compiles the result as follows:
The age of the puppy is: 7 

Example 2

In the following example, the age variable is not initialized, so it will fail at compile time:
Package COM . runoob . Test ; public class the Test { public void pupAge ( ) { int Age ; Age = Age + . 7 ; the System . OUT . the println ( " dog's age: " + Age ) ; } Public static void main ( String args [ ] ) { the Test Test = new new the Test ( ) ; Test . PupAge ( ) ; } }
The above example compiles the result as follows:
Test . Java : 4 : variable number might not have been initialized
Age = age + 7 ; ^ 1 error 
         

Instance variable

  • Instance variables are declared in a class, but outside of methods, constructors, and statement blocks;
  • When an object is instantiated, the value of each instance variable is followed;
  • Instance variables are created when the object is created and destroyed when the object is destroyed;
  • The value of the instance variable should be referenced by at least one method, constructor, or block of statements, so that the instance can get the instance variable information in some way;
  • Instance variables can be declared before or after use;
  • Access modifiers can modify instance variables;
  • Instance variables are visible to methods, constructors, or block statements in a class. In general, the instance variable should be private. You can make instance variables visible to subclasses by using access modifiers;
  • Instance variables have default values. The default value for numeric variables is 0, the default for boolean variables is false, and the default for reference type variables is null. The value of a variable can be specified at the time of the declaration, or it can be specified in the constructor;
  • Instance variables can be accessed directly through variable names. But in static methods and other classes, you should use the fully qualified name: ObejectReference.VariableName.

Examples

Employee.java File code:

Import java . Io . *; Public class Employee { // This instance variable is visible to subclasses Public String name ; // private variable, visible only in that class Private double salary ; // assign value to name in the constructor Public Employee ( String empName ) { name = empName ; } // Set the value of salary Public void setSalary ( double empSal ) { salary = empSal ; } // print the message public void with Printemp ( ) { the System . OUT . the println ( " Name: " + name ) ; the System . OUT . the println ( " Salary: " + the salary ) ; } public static void main ( String args [ ] ) { the Employee empOne = new new Employee ( " RUNOOB " ) ; EmpOne . SetSalary ( 1000 ) ; empOne . PrintEmp ( ) ; } }
The above example compiles the result as follows:
$ Javac Employee . Java
$ Java Employee Name : RUNOOB
 Salary : 1000.0
   

Class variable (static variable)

  • A class variable is also called a static variable, declared in the class as a static keyword, but must be outside the method constructor and block of statements.
  • Regardless of how many objects a class has created, the class has only one copy of the class variable.
  • Static variables are rarely used in addition to being declared as constants. Constants are variables declared as public / private, final, and static. Constant can not be changed after initialization.
  • Static variables are stored in static storage. Often declared as a constant, rarely use static declaration variables 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. However, for the user of the class, most static variables are declared as public.
  • The default value is similar to the instance variable. The numeric variable defaults to 0, the boolean default is false, and the reference type defaults to null. The value of a variable can be specified at the time of the declaration, or it can be specified in the construction method. In addition, static variables can also be initialized in static statement blocks.
  • Static variables can be accessed by: ClassName.VariableName .
  • When a class variable is declared as a public static final type, the class variable name is generally recommended to use uppercase letters. If the static variable is not a public and final type, its naming is consistent with the instance variable and the local variable naming.
Example:

Employee.java File code:

Import java . Io . *; Public class Employee { // salary is a static private variable Private static double salary ; // DEPARTMENT is a constant public static Final String the DEPARTMENT = " Developer " ; public static void main ( String args [ ] ) { the salary = 10000 ; the System . OUT . the println ( the DEPARTMENT + " average wage: " + the salary ) ; } }
The above example compiles the result as follows:
Average salary of developers : 10000.0
Note: If other classes want to access the variable, you can access: Employee.DEPARTMENT .
In this section we learned about Java variable types, and in the next section we will cover the use of Java modifiers.


EmoticonEmoticon