-3

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

3
  • 5
    Turn your head towards the right of your monitor and take a look at the Related section. Commented Feb 27, 2014 at 22:45
  • 1
    Have a look under "Related" on the right. How many questions are there about non-static things being referenced from a static context? One of them is bound to have a good answer for you. Commented Feb 27, 2014 at 22:45
  • When you use area on line 48, where do you expect it to come from? Commented Feb 27, 2014 at 22:50

1 Answer 1

0

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

What is static and why is it 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;
  } 

  public 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);
Sign up to request clarification or add additional context in comments.

6 Comments

what should i have instead? i have to use some argument.
i need the user to be enter the variable data.
With 12k, you should know that this question is a duplicate, and yet you do answer, Therefore, -1. (The answer is not very enlightening, also.)
@Ingo, If you really think that marking that as duplicated for someone who can fully comprehend the compiler message that state really nice an pass it to poorly explained answer is good choice for you i ok with me. But punishing someone for that he wants to provide a useful solution is just not right. Enjoy the day.
Yeah, but it's not really the right answer. The OP's problem is that he/she hasn't grasped what getters and setters are about. Unless you want to explain that, there's not much point in providing an answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.