Deval Khandelwal
Deval Khandelwal

Reputation: 3548

java importing from the same package

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!

Upvotes: 4

Views: 6214

Answers (6)

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

Upvotes: 0

dvrnaidu
dvrnaidu

Reputation: 151

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

Upvotes: 1

blalasaadri
blalasaadri

Reputation: 6188

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.

Upvotes: 13

ADTC
ADTC

Reputation: 10086

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();

Upvotes: 0

Hrishikesh
Hrishikesh

Reputation: 2053

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.

Upvotes: 0

Kick
Kick

Reputation: 4923

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

Upvotes: 0

Related Questions