Results 1 to 4 of 4
  1. #1
    purestr999 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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:
    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();
    			...
    			}
    		}
    		...
    	}
    }
    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?
    Thanks for helping me :)
    P.S. I know that perhaps Animal does not need to be a nested class, this was just an example.

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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.

  3. #3
    purestr999 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Classes with Different States/Modes

    Ok thanks for the reply
    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

Similar Threads

  1. jbutton states and events
    By ryuti in forum New To Java
    Replies: 30
    Last Post: 03-27-2012, 03:59 AM
  2. applets states in browsers
    By laxtrappa in forum Java Applets
    Replies: 5
    Last Post: 03-24-2012, 05:24 PM
  3. Association Classes for these classes
    By kevinkhan in forum New To Java
    Replies: 9
    Last Post: 03-21-2012, 05:22 PM
  4. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  5. JButton States Set to Pictures From Image Editor
    By wired-in=p in forum New To Java
    Replies: 10
    Last Post: 08-21-2011, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •