0

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?

1
  • Thinking that the best you can do is modify the field's scope to 'private' or package-level access (if the other file is outside of the current package). Commented Jun 15, 2015 at 1:23

1 Answer 1

2

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.

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.