Results 1 to 11 of 11
  1. #1
    jackrulesok is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Unhappy declaring problems with enums

    The following was asked of me and having a little problem with declaring enums in another class. The enum is set up correctly as I have used them before for GUI but doesnt seem to work when needing to call from it


    \**
    * A Route has a destination and a type. The type of the route can be
    * : local,airport or depot (set in enum class)

    public class Route
    {
    private String destination;
    private RouteType rType;


    /** creates a route given its destination as a String and its type
    * @param d represents the destination as a String
    * @param t represents type; clients should use the public static fields available in this class
    */
    public Route(String d, RouteType t)
    {
    destination = d;
    rType = t;
    }


    as it says in the comments it needs to be public static fields for type but i cant get it working.
    Any suggestions?
    Hope its clear enough!

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    have you tried it with just public? and is that your constructor? or an extended one?

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Show your RouteType class

    perhaps this pre-enum days pattern will help you:

    Java Code:
    public class RouteType {
      public static final RouteType TYPE_1 = new RouteType();
      public static final RouteType TYPE_2 = new RouteType();
      public static final RouteType TYPE_3 = new RouteType();
      
      // the constructor is private so noone else can create instances
      private RouteType() {
      }
    With this code, we're guaranteed that there's only 3 objects of RouteType in the JVM.

  4. #4
    jackrulesok is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Well the only real problem i'm having is when someone tries to create a new Route class they need to enter in the type (RouteType rType) but the software will not let them enter anything in (Im using BlueJ if that helps)

    Here is my enum class like someone asked for.....

    public enum RouteType
    {
    LOCAL("Local"), AIRPORT("Airport"), DEPOT("Depot");
    private String state;

    private RouteType(String st)
    {
    state = st;
    }
    }

    Thanks for your help guys,
    keep the suggestions coming!

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    but the software will not let them enter anything in
    i'm not sure i understand that... if you just type in the right code in Notepad and compile, does it work?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jackrulesok View Post
    Well the only real problem i'm having is when someone tries to create a new Route class they need to enter in the type (RouteType rType) but the software will not let them enter anything in (Im using BlueJ if that helps)

    Here is my enum class like someone asked for.....

    public enum RouteType
    {
    LOCAL("Local"), AIRPORT("Airport"), DEPOT("Depot");
    private String state;

    private RouteType(String st)
    {
    state = st;
    }
    }

    Thanks for your help guys,
    keep the suggestions coming!
    Suggestion: try using code tags when posting code. Doing this will make your code much more readable. To learn how, please see the link in my signature. For e.g.,
    Java Code:
    public enum RouteType
    {
        LOCAL("Local"), AIRPORT("Airport"), DEPOT("Depot");
        private String state;
        
        private RouteType(String st)
        {
            state = st;
        }
    }

    Next, please tell us the exact error message that is occurring, what makes this occur and what line it occurs on. Right now we don't have much information to go on.

    Next, you may want to give your RouteType enum a public String getState() method (or a toString method) so that the state String can be extracted if needed.

    Much luck!

  7. #7
    jackrulesok is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    oh rite, sorry. I am using BlueJ (the software). The code for the enum is fine, the problem is what i mentioned at first. The comment at the top is what is needed of me (@param t represents type; clients should use the public static fields available in this class). Confused about it all.
    grrrrr

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jackrulesok View Post
    oh rite, sorry. I am using BlueJ (the software). The code for the enum is fine, the problem is what i mentioned at first. The comment at the top is what is needed of me (@param t represents type; clients should use the public static fields available in this class). Confused about it all.
    grrrrr
    RouteType can be a public static inner class, well actually a public static inner enum. I'm not sure if that is what the instructor is looking for though.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Quite.
    That statement really doesn't make much sense.
    I see no public static anything there.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Quite.
    That statement really doesn't make much sense.
    I see no public static anything there.
    A nested enum is static by definition because the compiler treats it that way; consider it a compiler service ;-) Of course the OP needs to tell the compiler what enum to use when s/he wants to use one. Besides that I don't see a problem.

    kind regards,

    Jos

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    But the statement:
    "clients should use the public static fields available in this class"
    referring to RouteType makes no sense.

    From a teaching perspective, point out the public static fields in that enum. There aren't any.

Similar Threads

  1. why we are using enums in Java?
    By manish.anchan in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 04:41 PM
  2. declaring classes
    By coltragon in forum New To Java
    Replies: 17
    Last Post: 12-21-2009, 06:20 PM
  3. Declaring a Queue
    By rhm54 in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:02 AM
  4. Declaring Interface
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:41 AM
  5. Declaring Enumeration
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 05:59 PM

Posting Permissions

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