Java variable type

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. You can use commas to separate multiple variable declarations of the same type.
The following is a list of declarations for some variables. Note that some include the initialization process.
int a , b , c ;         // declares three int 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 = "Java" ; // declare and initialize the string s Double pi = 3 .14159 ; // declares double-precision floating-point variable pi char x = 'x' ;        // Declares that the value of the variable x is the character 'x'.
The types of variables supported by the Java language are:
  • Class variables: variables independent of methods, decorated with static.
  • Instance variables: Variables that are independent of methods, but have no static decoration.
  • Local variables: Variables in methods of the class.

Examples

public class Variable {
static int allClicks = 0 ; // class variables String str = " hello world " ; // instance variables public void method ( ) {
int i = 0 ; // local variable }
}

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 that declares it;
  • Local variables are allocated on the stack.
  • Local variables have no default value, so local variables 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.javademo.test ; public class Test {
public void pupAge ( ) {
int age = 0 ; age = age + 7; System.out.println(" Puppy's age is: " + age); }

public static void main(String [ ] args) {
Test test = new Test(); test.pupAge(); }
}
The above example compiled and run results are as follows:
The puppy's age is: 7 

Example 2

In the following example, the age variable is not initialized, so an error occurs at compile time:
package com.javademo.test ; public class Test {

public void pupAge() {
int age ; age = age + 7 ; System . out . println ( "The puppy's age is: " + age ) ; }

public static void main ( String [ ] args ) {
Test test = new Test ( ) ;
test.pupAge ( ) ; }
}
The above example compiled and run results 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 the instance variable should be referenced by at least one method, constructor, or block of statements so that the external 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 the class. In general, you should make the instance variable private. Instance variables can be made visible to subclasses by using access modifiers;
  • Instance variables have default values. The default value of a numeric variable is 0, the default value of a Boolean variable is false, and the default value of a reference-type variable is null. The value of the variable can be specified at the time of declaration, or it can be specified in the constructor;
  • Instance variables can be accessed directly by 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 this class private double salary ;
// Assign name to 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 printEmp() {
System.out.println(" Name: " + name); System.out.println(" Salary: " + salary); }

public static void main (String [ ] args) {
Employee empOne = new Employee( " Bubble " ); empOne.setSalary(1000); empOne.printEmp(); }
}
The above example compiled and run results are as follows:
$ javac Employee . java
$ java Employee name : Bubble
 salary : 1000.0
   

Class variables (static variables)

  • Class variables, also called static variables, are declared in the class as static keywords, 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 variable.
  • Static variables are rarely used other than being declared as constants. Constants are variables declared as public/private, final, and static. Constants cannot be changed after initialization.
  • Static variables are stored in static memory. Often declared as a constant, it is rare to use static declaration variables alone.
  • Static variables are created at the beginning of the program and destroyed at the end of the program.
  • Similar visibility to instance variables. However, in order to be visible to the user of the class, most static variables are declared as public.
  • The default is similar to the instance variable. The default value of a numeric variable is 0, the default value of the Boolean type is false, and the default value of the reference type is null. The value of the variable can be specified at the time of the declaration, or it can be specified in the constructor. 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 or a final type, it is named in the same way as the instance variable and the local variable.
Examples:

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 DEPARTMENT = " developer " ; public static void main ( String [ ] args ) {
salary = 10000 ; System . out . println ( DEPARTMENT + " average salary: " + salary ) ; }
}
The above example compiled and run results are as follows:
Average developer salary: 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. In the next section we will introduce the use of Java modifiers.

Comments