Latest web development tutorials

Java variable types

In the Java language, all variables must be declared before use. The basic format of variable declaration as follows:

type identifier [ = value][, identifier [= value] ...] ;

Format Description: type of Java data types. identifier is a variable name. You can use commas to declare multiple variables of the same type.

Here are some examples of variable declarations. Note that some contain the initialization process.

int a, b, c;         // 声明三个int型整数:a、 b、c
int d = 3, e, f = 5; // 声明三个整数并赋予初值
byte z = 22;         // 声明并初始化 z
String s = "w3big";  // 声明并初始化字符串 s
double pi = 3.14159; // 声明了双精度浮点型变量 pi
char x = 'x';        // 声明变量 x 的值是字符 'x'。

Java language supports variable types are:

  • Local variables
  • Member variables
  • Class variables

Java local variables

  • Local variables declared in a method, constructor or statement blocks;
  • Local variables when the method, constructor, or a block of statements to be executed to create, when they are executed, the variable will be destroyed;
  • Access modifier can not be used for local variables;
  • Local variables declared only in its methods, constructors or statement blocks visible;
  • Local variables are allocated on the stack.
  • Local variables do not have a default value, so the local variable is declared, must be initialized before they can be used.

Example 1

In the following examples, age is a local variable. Defined in pupAge () method, its scope would be limited to this method.

package com.w3big.test;

public class Test{ 
   public void pupAge(){
      int age = 0;
      age = age + 7;
      System.out.println("小狗的年龄是: " + age);
   }
   
   public static void main(String args[]){
      Test test = new Test();
      test.pupAge();
   }
}

The above examples compiled results are as follows:

小狗的年龄是: 7

Example 2

In the example below age variable is not initialized, so at compile time error:

public class Test{ 
   public void pupAge(){
      int age;
      age = age + 7;
      System.out.println("小狗的年龄是 : " + age);
   }
   
   public static void main(String args[]){
      Test test = new Test();
      test.pupAge();
   }
}

The above examples compiled results are as follows:

Test.java:4:variable number might not have been initialized
age = age + 7;
         ^
1 error

Instance variables

  • Instance variables declared in a class, but in the method of construction methods and statements outside the block;
  • When an object is instantiated, the value of each instance variable is determined to follow;
  • In the instance variable is created when an object is created, when the object is destroyed destruction;
  • Instance variable's value should be at least a method, constructor or statement block references, so that the external instance variables can obtain information through these means;
  • Instance variables can be declared before use or after use;
  • Access modifier can be modified instance variables;
  • For instance variable class method, constructor or statement block it is visible. Under normal circumstances should be made private instance variables. By using access modifiers can make instance variables visible to a subclass;
  • Instance variables have default values. The default value of numeric variable is 0, the default value of a Boolean variable is false, the default value of the reference type variable is null. Value of the variable can be specified at the time of declaration, it can also be specified in the constructor;
  • Instance variables can be directly accessed by the variable name. However, static methods in other classes, you should use the fully qualified name: ObejectReference.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 above examples compiled results are as follows:

name  : Ransika
salary :1000.0

Class variables (static variables)

  • Class variables, also known as a static variable in the class to declare the static keyword, but the outside must be in the process of construction method and block.
  • Whether a class to create a number of objects, classes only have one copy of the class variable.
  • In addition to the static variable is declared as constants rarely used. Constants are declared as variables public / private, final and static type. After initialization constants can not be changed.
  • Static variables are stored in static memory. Often it declared as a constant, rarely used alone static variable declarations.
  • Static variables when the program begins to create, destroy at the end of the program.
  • And instance variables have similar visibility. But to the user of the class can be seen, most of the static type variable is declared as public.
  • Default and instance variables similar. Numeric variable The default value is 0, the default Boolean value is false, the reference type the default value is null. Value of the variable can be specified at the time of declaration to be specified in the constructor. In addition, static variables can also be initialized in the static statement blocks.
  • Static variables may be: ClassName.VariableName of access.
  • Class variable is declared as public static final type, class variable name must use uppercase letters. If the static variable is not public and final type, consistent naming and naming its instance variables and local variables.

Example:

import java.io.*;
public class Employee {
    //salary是静态的私有变量
    private static double salary;
    // DEPARTMENT是一个常量
    public static final String DEPARTMENT = "开发人员";
    public static void main(String args[]){
    salary = 10000;
        System.out.println(DEPARTMENT+"平均工资:"+salary);
    }
}

The above examples compiled results are as follows:

开发人员平均工资:10000.0

Note: If you want to access other types of this variable, it can be accessed: Employee.DEPARTMENT.

This chapter we learn Java variable types, the next section we will introduce the use of Java modifiers.