Results 1 to 11 of 11
Thread: declaring problems with enums
- 04-29-2010, 07:09 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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!
- 04-29-2010, 07:33 PM #2
have you tried it with just public? and is that your constructor? or an extended one?
- 04-29-2010, 08:36 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Show your RouteType class
perhaps this pre-enum days pattern will help you:
With this code, we're guaranteed that there's only 3 objects of RouteType in the JVM.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() { }
- 04-29-2010, 10:33 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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!
- 04-29-2010, 11:23 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
i'm not sure i understand that... if you just type in the right code in Notepad and compile, does it work?but the software will not let them enter anything in
-
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!
- 04-30-2010, 12:10 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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
-
- 04-30-2010, 09:56 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Quite.
That statement really doesn't make much sense.
I see no public static anything there.
- 04-30-2010, 10:13 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 04-30-2010, 10:16 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
why we are using enums in Java?
By manish.anchan in forum New To JavaReplies: 7Last Post: 01-08-2010, 04:41 PM -
declaring classes
By coltragon in forum New To JavaReplies: 17Last Post: 12-21-2009, 06:20 PM -
Declaring a Queue
By rhm54 in forum New To JavaReplies: 1Last Post: 03-21-2008, 05:02 AM -
Declaring Interface
By Java Tip in forum Java TipReplies: 0Last Post: 11-08-2007, 08:41 AM -
Declaring Enumeration
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks