0

How can I handle pass an enum from a class to another class. The passed enum inits another enum in Test2 class.

Here is an explanation of my problem. I know enum is final and cannot be override. So figure out the this.Jun2 = E; The code is just explanation.

class Test1{
 enum Junk{A,B,C}; 
 Test2.loader(Junk);

}

class Test2{
 enum Junk2{};
 public static void loader(enum E) 
  this.Junk2 = E;
}
...other jobs based on Junk2;

The purpose of, Test2 class as an abstract class that handle enums which are desingned from extended classes like Test class

2
  • 1
    Junk and Junk2 are two different types and cannot be assigned to each other. Can you explain a bit more what you are trying to achive, problably there is another solution to your problem. Btw an enum can implement an interface. Commented Oct 6, 2014 at 11:06
  • I want to desing an abstract class that handle enums which are desingned from extended classes Commented Oct 6, 2014 at 11:09

1 Answer 1

2

You specify the enum type in loader's signature (you'll need to give access to the enum to both classes):

 public static void loader(Junk junk)

You've shown a second, separate enum in Test2. It would probably make more sense for Test2 to use Junk as well, not a separate enum. If you use a separate one, you'll have to have a mapping between the values (which you'd usually implement as a method on the enum, e.g., Junk2.fromJunk(...)), which is a maintenance issue.


Side note: In the above, I changed the name of the argument from E to junk. In Java, the overwhelming convention is that argument names start with a lower case letter. Separately, the name of the argument should reflect what it is in some way. E might suggest "enum," but which one? In general, without going overboard, make names specific enough to be meaningful in some way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.