java importing from the same package

import, java, methods, package

Solution

You can only import a function from another class if that function is static; so for example this will work:

public class Gets {
    public static void foo() {}
}

and then import it:

import static acs.Gets.foo;

If it isn't static however you won't be able to import it.

EDIT: As was pointed out in the comments, using static imports can make code more difficult to read, so they should be used with caution. They can be useful if used correctly though.

Problem

I am a newbie in java and googling did not help me. So, bear with me...:P I created a package `acs`. This package has got the class `Gets` which has the method `foo()`. Now, I created another class in the same package and tried to call the method `foo()`. Of course this will work - `(new Gets()).foo();`. But I added an import `import acs.Gets;` and simply tried to use the method directly - `foo();` as described in http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html. But unfortunately, the code did not work. Please tell me where am I going wrong. Any help will be appreciated!

Original source