hyperneutrino
hyperneutrino

Reputation: 5425

Java import static method but not field

Take the following class for example:

public final class ClassName {
    public static final void TEST() {}
    public static final Object TEST;
}

Now, from another file, I want to import static ClassName.TEST(), but not ClassName.TEST.

How would I go about importing a method but not an identically named field, or vice versa?

Upvotes: 0

Views: 168

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280141

You can't.

import statements are completely a compile time concept. They don't do anything at run time. They allow you to use the simple name instead of a fully qualified name of types, or their members.

When you use

import static com.example.ClassName.TEST;

you're telling the compiler that you will want to use the simple name TEST from the type com.example.ClassName without qualification. What member it refers to doesn't matter*.

Java will be smart enough to determine if you mean to use the method or field based on its context (where and how it's used).

* except where obscuring might happen.

Upvotes: 2

Related Questions