Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Elevator problem (Statics)

 
Alex Munoz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How could I make my code compatible with static context?

I am also getting errors about being unable to find the symbol targetFloor. How could I address that?

I haven't gotten around to setting how the elevator moves, so ignore the last method.
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Munoz wrote:
How could I make my code compatible with static context?
I am also getting errors about being unable to find the symbol targetFloor. How could I address that?

Check out http://www.coderanch.com/t/597438/java/java/non-static-variable-totals-cannot, especially the link provided by Jesper in his reply
You have declared targetFloor inside the method setTargetFloor. Because of this, the scope is limited only to that method.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Munoz wrote:How could I make my code compatible with static context?

What do you mean by that? Do you get an error message when you try to compile this that has something to do with static?

Alex Munoz wrote:I am also getting errors about being unable to find the symbol targetFloor. How could I address that?

Inside the method setTargetFloor(), on line 24, you declare a local variable named targetFloor. This variable only exists inside the method - it's a local variable.

Inside the method setDirection(), on lines 31 and 35, you're referring to a variable named targetFloor. That variable doesn't exist there.
 
Alex Munoz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Alex Munoz wrote:How could I make my code compatible with static context?

What do you mean by that? Do you get an error message when you try to compile this that has something to do with static?


Yes, the error says, "non-static method setDirection() cannot be referenced from a static context"

I fixed the error concerning targetFloor
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
General rule of thumb about things static. If you have a good explanation why something should be static, then it probably should be static. If you haven’t got a good explanation, then making anything static is a serious design error. “It compiles like that,” is a very bad explanation.
Using ints instead of enumerated types, eg direction, is a bad move. It might be permissible in C, but it is not at all good design in Java.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Munoz wrote:How could I make my code compatible with static context?

Easiest is usually to create a object, and then call methods on that object.

I'll bet dollars to doughnuts that the reason you're getting these error messages about 'static context' is because you're trying to call your class methods directly from main(); and that's NOT how you should do things.

As Campbell said, making things static is usually a BAD idea. In the case of main() you have no choice; but you should generally avoid using it anywhere else unless you have a good reason for doing so.

Winston
 
Alex Munoz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm confused as to how I implement this. What needs to change?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic