|
-
Jun 11th, 2002, 03:38 AM
#1
Thread Starter
PowerPoster
Public Enum??
Is there any way to declare a public enum in Java, that can be accessed by at least 2 classes without duplicating the code??
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Jun 11th, 2002, 06:49 AM
#2
Unfortunately, enums are nonexistant in Java. The best you can do is use static final variables.
Code:
class MyEnums
{
public static final int MOVE_FIRST = 0
public static final int MOVE_PREVIOUS = 1
public static final int MOVE_NEXT = 2
public static final int MOVE_LAST = 3
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 11th, 2002, 06:22 PM
#3
Thread Starter
PowerPoster
Yep, ta. I understand that, but how do I make that available to 2 separate classes? Should the enum class be its own compiled class, or should I just duplicate it as an internal class of each of the other 2?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Jun 11th, 2002, 06:31 PM
#4
Well, from a maintenance stand point, it would seem better to compile it as a separate class or interface. Then any changes made later will just flow down to the other classes that use it.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 11th, 2002, 06:43 PM
#5
Thread Starter
PowerPoster
Ok, so to access the constants, I would need to instantiate the class in any class I want to use it in?
And can I use that class as a parameter to restrict inputs, like an enum in VB, or is it all just for show really?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Jun 11th, 2002, 06:51 PM
#6
Well, if the variables are static, then you just need to reference them by class name. ie,
Code:
public class Test
{
public static void main(String[] args)
{
System.out.println(RJsEnumClass.SOME_ENUM_VALUE);
}
}
And have your enum class :
Code:
public class RJsEnumClass
{
public static int SOME_ENUM_VALUE = 0;
//blah, blah, blah...
}
And I think you'll have to restrict the input ranges from within the method you are calling.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|