java : non-static variable cannot be referenced from a static context errors

Java:非静态变量不能从静态方面的错误引用

Tags: java static
标签: java static

问题 (Question)

The following code should let a user input the square feet and stories of a building. I keep having problems with the code in main and for the life of me I cannot figure out why.

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)");

}
}

The problem im having are on lines 48 and 50 and it displays the error;

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);

Thanks for the help

下面的代码应该让用户输入平方英尺的建筑的故事。我把有问题的代码中的主、我的生活,我不明白为什么。

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);

谢谢你的帮助

最佳答案 (Best Answer)

The problem is with area and floors. You use them in static main method. That is not allowed.

What is static and why it is so important ?

In Java everything you create has declared scope of visibility. You allocate files in packages, in files you create classes and in those you store members.

The general concept is that a class is an template from witch you create objects. When you have object you can set the values to its field and call his methods and the action will be reflected only inside the object and all value will be assigned to object. When you use the static key word you mark a member to be no longer part of object but only element of class common to all object.

Lets see the example

class Sample {

  private int instance_value;
  private static int class_value;

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

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

}

We create now two objects of class Sample.

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

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

The output will be:

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

The reason why in class_value has changed to 4 is because it is static and is the same for all element that use it.

Note, you can access the static member from non static, what you can not do is to access the non static from static.

Instead of using the names that you have declared in class body set the values in main

You have this

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

you may want to have this

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

Or define them once again to cover the scope.

double area = 50.0;
int    storied = 5;

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

问题是areafloors。您使用它们static主要方法。这是不允许的。

什么是静态的,它为什么如此重要?

在Java你所创造的一切都已经宣布可见性的范围。你分配文件包中的文件,在你创建类和那些你商店的成员

一般的概念是,一类是从女巫你创建对象的模板。当你有了目标你可以设定值的字段和呼叫他的方法和行动将只反映对象内部的所有值将被指定为对象。当你使用你的关键字标记的静态成员不再对象而是阶级共同所有的对象只元件部分。

让我们看看例子

class Sample {

  private int instance_value;
  private static int class_value;

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

  pubic 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是因为它是静态的,是所有元素,使用相同的。

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

而不是使用的名称,您在类体宣布主设定值

你有这个

 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);

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22082268