Results 1 to 4 of 4
- 04-25-2012, 09:55 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Classes with Different States/Modes
Hello, I am looking for a good way to make a class with different states/modes. I am pretty sure the correct way to do this is with enums. My problem is that enums can't be used in nested classes (due to enums being static).
For example:
The enum ZooState works exactly as I want it to work. The problem is that AnimalState in this example is illegal in Java. I could put the enum definition (AnimalState) inside the outer class (Zoo), but is there a better way to do this when the enum is only relevant to the nested class?Java Code:public class Zoo { private enum ZooState {OPEN, CLOSED, MAINTENANCE}; private ZooState zooState; public void openZoo(){ switch(zooState){ case CLOSED: zooState = OPEN; ... } } ... public class Animal { private enum AnimalState {SLEEPING,EATING,PLAYING}; private AnimalState state; public void petAnimal(){ switch(state){ case SLEEPING: petWake(); ... } } ... } }
Thanks for helping me :)
P.S. I know that perhaps Animal does not need to be a nested class, this was just an example.
- 04-25-2012, 10:04 PM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Classes with Different States/Modes
I would first ask if you REALLY need the nested class. But beyond that, there's no way around the enums all having to be in the outer class other than making the inner class static, which I don't think you really want or need.
- 04-25-2012, 10:53 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: Classes with Different States/Modes
Ok thanks for the reply
-.gif)
I know that in this example you wouldn't need a nested class, but obviously sometimes you need nested classes and sometimes those classes will need different states/modes. I just thought that maybe Java had some better way to solve this problem.
- 04-26-2012, 09:49 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Classes with Different States/Modes
Non-static non-anonymous nested classes are quite a rare thing.
In most of the stuff I produce, if it is a class that can leave its parent then it shouldn't be a non-static nested one.
In your case I really wouldn't nest it.
It's part of your Zoo model, so stick all the bits into a zoo.model package.Please do not ask for code as refusal often offends.
Similar Threads
-
jbutton states and events
By ryuti in forum New To JavaReplies: 30Last Post: 03-27-2012, 03:59 AM -
applets states in browsers
By laxtrappa in forum Java AppletsReplies: 5Last Post: 03-24-2012, 05:24 PM -
Association Classes for these classes
By kevinkhan in forum New To JavaReplies: 9Last Post: 03-21-2012, 05:22 PM -
Classes Help!!! two classes with object question.
By stuckonjava in forum New To JavaReplies: 16Last Post: 02-10-2012, 01:39 AM -
JButton States Set to Pictures From Image Editor
By wired-in=p in forum New To JavaReplies: 10Last Post: 08-21-2011, 04:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks