Home > Java > javaTutorial > Java's three variable types, local variables, instance variables, and class variables

Java's three variable types, local variables, instance variables, and class variables

php是最好的语言
Release: 2018-07-30 10:14:07
Original
2534 people have browsed it

The variable types supported by the Java language are:

局部变量:类的方法中的变量。
实例变量:独立于方法之外的变量,没有static修饰。
类变量:独立于方法之外的变量,用static修饰。
Copy after login
Copy after login
Local variables

Local variables have the following characteristics:

1.局部变量声明在方法、构造方法或者语句块中;
2.局部变量在方法、构造方法、或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁;
3.访问修饰符不能用于局部变量;
4.局部变量只在声明它的方法、构造方法或者语句块中可见;
5.局部变量是在栈上分配的。
6.局部变量没有默认值,所以局部变量被声明后,必须经过初始化,才可以使用。
Copy after login

Key points:
Local variables It cannot have access modifiers, is allocated on the stack, and must be initialized.

In the following example, the age variable is not initialized, so an error will occur during compilation:

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

The compilation and running results of the above example are as follows:

PuppyTest.java:4: 可能尚未初始化变量 age
      age = age + 7;
            ^
1 错误
Copy after login
Instance variable

Instance variables have the following characteristics:

1.实例变量声明在一个类中,但在方法、构造方法和语句块之外;
2.当一个对象被实例化之后,每个实例变量的值就跟着确定;
3.具有默认初始值,数值型变量默认值是0,布尔型默认值是false,引用类型默认值是null。变量的值可以在声明的时候
指定,也可以在构造方法中指定。
4.实例变量在对象创建的时候创建,在对象被销毁的时候销毁,其存在于对象所在的对内存中;
5.实例变量的值应该至少被一个方法、构造方法或者语句块引用,使得外部能够通过这些方式获取实例变量信息;
6.实例变量可以声明在使用前或者使用后;
7.访问修饰符可以修饰实例变量;
8.实例变量对于类中的方法、构造方法或者语句块是可见的。一般情况下应该把实例变量设为私有。通过使用访问修饰符
可以使实例变量对子类可见;
9.变量的值可以在声明时指定,也可以在构造方法中指定;
10.实例变量可以直接通过变量名访问。但在静态方法以及其他类中,就应该使用完全限定名:
ObejectReference.VariableName。
Copy after login

Key points: can be initialized by default, exist in the memory where the object is located, and the declaration period is the object period.

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 );
      System.out.println("薪水 : " + salary);
   }   public static void main(String[] args){
      Employee empOne = new Employee("RUNOOB");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}
Copy after login
Class variables (static variables)

Class variables are also called static variables. They are declared with the static keyword in the class, but they must be Outside constructors and statement blocks.
Class variables have the following characteristics:

1.无论一个类创建了多少个对象,类只拥有类变量的一份拷贝。
2.静态变量除了被声明为常量外很少使用。常量是指声明为public/private,final和static类型的变量。常量初始化
后不可改变。
3.静态变量储存在静态存储区。经常被声明为常量,很少单独使用static声明变量。
4.静态变量在第一次被访问时创建,在程序结束时销毁。
5.与实例变量具有相似的可见性。但为了对类的使用者可见,大多数静态变量声明为public类型。
6.默认值和实例变量相似。数值型变量默认值是0,布尔型默认值是false,引用类型默认值是null。变量的值可以在声明的
时候指定,也可以在构造方法中指定。此外,静态变量还可以在静态语句块中初始化。
7.静态变量可以通过:ClassName.VariableName的方式访问。
8.类变量被声明为public static final类型时,类变量名称一般建议使用大写字母。如果静态变量不是public和final
类型,其命名方式与实例变量以及局部变量的命名方式一致。
Copy after login

Key points: All objects share a copy of the class variable, are often declared as constants, are stored in static storage, and the declaration cycle lasts for the entire After the program ends, it can be accessed directly through the class name.

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);
    }
}
Copy after login

Note: If other classes want to access this variable, they can access it like this: Employee.DEPARTMENT.

The variable types supported by the Java language are:

局部变量:类的方法中的变量。
实例变量:独立于方法之外的变量,没有static修饰。
类变量:独立于方法之外的变量,用static修饰。
Copy after login
Copy after login

Related articles:

[Java Getting Started Notes] Java Language Basics (2): Constants, Variables and Data Type

JAVA Tutorial | Chapter 3 Variable Type

Related video:

The difference between static variables and member variables -JAVA Beginner's Video Tutorial

The above is the detailed content of Java's three variable types, local variables, instance variables, and class variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template