File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Elevator problem (Statics) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 10491
    
    9

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.


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

Joined: Aug 16, 2005
Posts: 14278
    
  21

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 8 API documentation
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: 39478
    
  28
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: 8049
    
  22

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?
Articles by Winston can be found here
Alex Munoz
Greenhorn

Joined: Oct 26, 2012
Posts: 10
I guess I'm confused as to how I implement this. What needs to change?
 
 
subject: Elevator problem (Statics)