4

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!

8
  • 2
    Can you show the exact code? Commented Jan 31, 2014 at 13:32
  • 2
    What error did it give? Commented Jan 31, 2014 at 13:32
  • Importing will not let you call the method directly - it just means you don't need to specify the full package name. You still need an object instance to invoke the method on. Or if it is a static method, you need to reference the class. Commented Jan 31, 2014 at 13:32
  • You don't need to add the import if the class is in the same package. Commented Jan 31, 2014 at 13:33
  • So what code shall I use @Trenin ? Commented Jan 31, 2014 at 13:33

6 Answers 6

13

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.

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

3 Comments

static imports can easily lead to confusion and must be avoided in favor of interfaces or regular imports.
Whether they must be avoided is a question of opinion; it is true however that they can lead to confusion, so I have added that warning to my reply.
Hey actually got it now..+1 for explaining about static methods.
1

make foo() method as static and access it using classname.foo();

Comments

0

You can't access the method of another class without its object whether both the class exist in same package or not.

Comments

0

Java is Object oriented. You need to create an object of the class for you to invoke methods of the class.

So something like

new Gets().myFunction();

is bound to work. The only exception to this rule is to create a static method. You can find out when to create static methods and if they suit your requirement, but to access a method of a class, you need an object.

Comments

0

You would still need to create an object to access the non-static method:

(new Gets()).foo();

And you will still need to declare the class to access a static method:

Gets.staticFoo();

You don't need to import a class from the same package. It is superfluous as all classes in a package are already visible to each other.

Also, you are also confusing yourself about how imports work. Importing a class does not mean that its method or fields become accessible without qualification.

The reason why one imports a class from another package, is so that one does not have to declare the package everywhere in code. Instead of writing:

acs.anotherpkg.Blah testblah = new acs.anotherpkg.Blah();

You can simply write:

import acs.anotherpkg.Blah;
...
Blah testblah = new Blah();

Comments

0

THERE ARE 3 WAYS TO RESOLVE THIS.

  1. Your import allows the use of an instance (non-static) method in combination with an Object of the Gets Class, like this:
Var random = new Gets(); //or Gets random = new Gets();  /*these both have the same effect.*/

//Then use the method like this: 

random.foo() //or System.out.print(random.foo());

/*
depending on whether your method returns a Primitive Type or Void, and its function.
*/ 
  1. The 2nd way to resolve this is to make foo() a static method, so it becomes a Class Method, and make it "default" or "public" to simply use the name of the class combined with the method name to call it. Like this:
Gets.foo(); //simple as that
  1. The 3rd way to resolve this is to use the "extends" keyword on your second class to make it a Subclass of the Gets class. This allows you to INHERIT all methods from the Gets class and use them without imports and without creating Objects of the Gets Class. Like this:
public class acs extends Gets{ 

public static void main(String[] args){

 /* method called without Objects or Class references. */

foo();

} // ends Main() 

}// ends acs() Class

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.