【问题标题】:java : non-static variable cannot be referenced from a static context errors [duplicate]java:无法从静态上下文错误中引用非静态变量[重复]
【发布时间】:2014-02-27 22:44:51
【问题描述】:

以下代码应让用户输入建筑物的平方英尺和楼层。我一直遇到 main 中的代码问题,而且我一生都无法弄清楚原因。

import java.util.*;

public class Building
{
static Scanner console = new Scanner(System.in);

double area, squarefootage;
int floors, stories;
char ans

void get_squarefootage()
{
System.out.println ("Please enter the square footage of the floor.");
area = console.nextDouble();
}

void set_squarefootage(double area)
{
squarefootage = area;
}

void get_stories()
{
System.out.println ("Please enter the number of floors in the building.");
floors = console.nextInt();
}

void set_stories(int floors)
{
stories = floors;
}

void get_info()
{
System.out.println (" The area is: " + squarefootage + " feet squared");
System.out.println (" The number of stroies in the building: " + stories + " levels");
}

public static void main(String[] args)
   {

   Building b = new Building();
   b.get_squarefootage();
   b.set_squarefootage(area);
   b.get_stories();
   b.set_stories(floors);
   System.out.println ("---------------");
   b.get_info();
   System.out.println ("Would you like to repeat this program? (Y/N)");

}
}

我遇到的问题在第 48 行和第 50 行,它显示错误;

Building.java:48: error: non-static variable area cannot be referenced from a static context
   b.set_squarefootage(area);
                       ^
Building.java:50: error: non-static variable floors cannot be referenced from a static context
   b.set_stories(floors);

感谢您的帮助

【问题讨论】:

  • 将头转向显示器右侧,查看Related 部分。
  • 在右侧的“相关”下查看。关于从静态上下文中引用的非静态事物有多少问题?其中之一一定会给你一个很好的答案。
  • 当你在第 48 行使用area 时,你期望它来自哪里?

标签: java static


【解决方案1】:

问题在于areafloors。你在static main 方法中使用它们。这是不允许的。

什么是static,为什么它如此重要?

在 Java 中,您创建的所有内容都声明了可见范围。您在包中分配文件,在您创建类的文件中以及在store members 中分配文件。

一般概念是,类是您创建对象的模板。当您拥有对象时,您可以为其字段设置值并调用他的方法,并且该操作将仅反映在对象内部,并且所有值都将分配给对象。当您使用静态关键字时,您将成员标记为不再是对象的一部分,而只是所有对象共有的类的元素。

让我们看看例子

class Sample {

  private int instance_value;
  private static int class_value;

  Sample(int first, int second) {
     instance_value = first;
     class_value   = second;
  } 

  public void print() {
     System.out.printf("instance_value=%i, class_value=%i\n",instance_value, class_value);
  }      

}

我们现在创建两个 Sample 类的对象。

 Sample objectOne = new Sample(1,2);
 objectOne.print();

 Sample objectTwo = new Sample(3,4);
 objectOne.print();

输出将是:

instance_value=1, class_value=2
instance_value=1, class_value=4

class_value 改成 4 的原因是因为它是静态的,对于所有使用它的元素都是一样的。

注意,你可以从非静态访问静态成员,你不能做的是从静态访问非静态。

不要使用您在类主体中声明的名称,而是在 main 中设置值

你有这个

 b.set_squarefootage(area);
 b.set_stories(stories);

你可能想要这个

 b.set_squarefootage(50.0);
 b.set_stories(5);

或者再次定义它们以覆盖范围。

double area = 50.0;
int    storied = 5;

 b.set_squarefootage(area);
 b.set_stories(stories);

【讨论】:

  • 我应该用什么代替?我必须使用一些论据。
  • 我需要用户输入变量数据。
  • 使用 12k,您应该知道这个问题是重复的,但您确实回答了,因此,-1。 (答案也不是很有启发性。)
  • @Ingo,如果您真的认为对于可以完全理解编译器消息的人来说将其标记为重复,那么将其传递给解释不清的答案对您来说是一个不错的选择,我同意。但是,为了提供有用的解决方案而惩罚某人是不对的。享受这一天。
  • 是的,但这并不是真正的正确答案。 OP的问题是他/她没有掌握getter和setter的含义。除非您想解释那个,否则提供答案没有多大意义。
猜你喜欢
  • 2018-01-04
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多