user445338
user445338

Reputation:

Enumeration enum in Java

I'm having trouble understanding a question. The question is: Code a statement that will make the constants in the Terms enumeration available to a class without qualification

This is my code so far;

public enum Terms {

    NET_30_DAYS, NET_60_DAYS, NET_90_DAYS; 
    //either name() or super() can be used

     @Override
     public String toString() {
         String s = "Net due " + name() + " days"; 
         return s;
     }
}

Upvotes: 1

Views: 367

Answers (2)

Kal
Kal

Reputation: 24910

You're probably looking for

import static package.Terms.*;

public class Foo {

     public void aMethod() {
             System.out.println(NET_30_DAYS);
             // you can do the above instead of System.out.println(Terms.NET_30_DAYS);
     }
}

There is one thing to note here. If your Terms.java is in a default package, there is not a way to do a static import on it.

Upvotes: 1

RonK
RonK

Reputation: 9652

I think that they refer to static import.

Example:

import static mypackage.Term.*;

This will enable you to use your code like:

public void doSomething(Term term)
{
    if (NET_30_DAYS.equals(term))
    {
        ...
    }
    else if ...
}

Upvotes: 3

Related Questions