This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Elevator problem (Statics) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Elevator problem (Statics)" Watch "Elevator problem (Statics)" New topic
Author

Elevator problem (Statics)

Alex Munoz
Greenhorn

Joined: Oct 26, 2012
Posts: 10


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

Joined: Jul 26, 2007
Posts: 8439

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.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12928
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Alex Munoz
Greenhorn

Joined: Oct 26, 2012
Posts: 10
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

Joined: Oct 13, 2005
Posts: 32694
    
    4
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

Joined: Mar 17, 2011
Posts: 4756
    
    7

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


Isn't it funny how there's always time and money enough to do it WRONG?
Alex Munoz
Greenhorn

Joined: Oct 26, 2012
Posts: 10
I guess I'm confused as to how I implement this. What needs to change?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Elevator problem (Statics)
 
Similar Threads
learning how to use classes
wanted help in the practice program--"Cycling"
HAS-A relationship?
Abbott's Revenge
java.lang.NoSuchMethodError ???????