Results 1 to 6 of 6

Thread: Public Enum??

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    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]
    -----------------------------------------

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  5. #5

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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
  •  



Click Here to Expand Forum to Full Width