English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

我们可以在Java中对非静态字段进行静态引用吗?

Java中的类将具有三种变量,即静态(类),实例和局部变量。

  • 局部变量-这些变量属于方法/块/构造函数并在其中声明/定义。这些变量的范围位于方法(或块或构造函数)内,执行后将被销毁。

  • 实例变量-这些变量属于类的实例(对象)。这些在类内但在方法外声明。这些在实例化类时初始化。可以从该特定类的任何方法,构造函数或块中访问它们。

    您必须使用对象访问实例变量。即,要访问实例变量,您需要创建该类的对象,并使用该对象,您需要访问这些变量。

  • 类/静态变量-类/静态变量属于类,就像实例变量一样,它们在类内在任何方法之外声明,但使用static关键字声明。

    它们可以在编译时访问,您可以在实例化之前/不实例化类时进行访问,整个类中只有一个静态字段的副本可用,即,静态字段的值在所有对象中都相同。您可以使用static关键字定义一个静态字段。

对非静态变量的静态引用e

如上 ,使用类名称引用(访问)静态变量。

System.out.println(MyClass.data);

即使用静态引用引用变量意味着使用类名称引用。

但是,要访问实例变量,必须创建一个对象,实例化之前,这些对象在内存中不可用。

因此,您不能在Java中静态引用非静态字段(变量)。如果还是这样,请尝试这样做,从而生成一个编译时错误,提示“无法从静态上下文中引用非静态变量数学”。

示例

遵循Java程序,接受用户标记并确定他是否晋升。

在这里,我们从静态方法wasPromroted()中,我们直接访问实例变量(仅通过指定其名称即可,就像对静态变量一样)。由于不允许这样做,将产生编译时错误。

import java.util.Scanner;
public class StudentMarks {
   Scanner scan1 = new Scanner(System.in);
   private double math;
   private double science;
   private double english;
   public StudentMarks(double math, double science, double english) {
      this.math = math;
      this.science = science;
      this.english = english;
   }
   public static boolean wasPromroted(StudentMarks marks) {
      if(math>=85 && science>=75 && english>=65) {
         return true;
      }
      return false;
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your math score: ");
      double math = sc.nextDouble();
      System.out.println("Enter your science score: ");
      double science = sc.nextDouble();
      System.out.println("Enter your english score: ");
      double english = sc.nextDouble();
      StudentMarks marks = new StudentMarks(math, science, english);
      boolean bool = wasPromroted(marks);
      if(bool) {
         System.out.println("Congratulations you've got promoted");
      } else {
         System.out.println("Sorry try again");
      }
   }
}

输出结果

StudentMarks.java:16: error: non-static variable math cannot be referenced from a static context
   if(math>=85 && science>=75 && english>=65)
^
StudentMarks.java:16: error: non-static variable science cannot be referenced from a static context
   if(math>=85 && science>=75 && english>=65)
^
StudentMarks.java:16: error: non-static variable english cannot be referenced from a static context
   if(math>=85 && science>=75 && english>=65)
^
3 errors

为了使该程序正常工作,要么需要将实例变量声明为静态,要么应使用方法中的对象对其进行引用。

import java.util.Scanner;
public class StudentMarks {
   Scanner scan1 = new Scanner(System.in);
   private double math;
   private double science;
   private double english;
   public StudentMarks(double math, double science, double english) {
      this.math = math;
      this.science = science;
      this.english = english;
   }
   public static boolean wasPromroted(StudentMarks marks) {
      if(marks.math>=85 && marks.science>=75 && marks.english>=65)
      return true;
      return false;
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your math score: ");
      double math = sc.nextDouble();
      System.out.println("Enter your science score: ");
      double science = sc.nextDouble();
      System.out.println("Enter your english score: ");
      double english = sc.nextDouble();
      StudentMarks marks = new StudentMarks(math, science, english);
      boolean bool = wasPromroted(marks);
      if(bool) {
         System.out.println("Congratulations you've got promoted");
      } else {
         System.out.println("Sorry try again");
      }
   }
}

输出结果

Enter your math score:
89
Enter your science score:
85
Enter your english score:
86
Congratulations you've got promoted
猜你喜欢