Reputation: 13108
Simple question but even though googled it a lot I could not find the answer.
Is it possible to import a class outside a package?
Let's say I have 2 folders A
and B
with a .java
file in each, is it possible by using the clause import
to import the class contained in A
? import A.Aclass
? or it's mandatory using package syntax whenever there is the keyword import
?
Upvotes: 4
Views: 85199
Reputation: 32627
Well, if the class is defined to have a package a;
then you need to import the class with the package name. If you have two packages which contain a class with the same name, then in your class which needs to invoke each of them, you will need to use a fully-qualified name. For example:
import a.Foo;
import b.Foo;
public class Bar
{
public static void main(String[] args)
{
a.Foo aFoo = new a.Foo();
b.Foo bFoo = new b.Foo();
}
}
Alternatively, if you have two packages with classes of the same name, you can simply skip importing them, but rather -- using them by their fully-qualified names (FQN-s).
If the class does not have a package ...;
, then simply import it as:
import Foo;
However, if you have two packages (from different libraries) which contain classes with identical FQN-s, then the first one on the classpath will be picked.
Please, bear in mind that the convention for naming packages is to use lowercase letters and for classes -- the name should start with an upper case letter for each word in the class' name.
Upvotes: 1
Reputation: 18499
Yes, you have to use package syntax.
importing all class inside folder A
.
import com.pack.A.*;
importing specific class inside folder A
.
import com.pack.ClassName;
Upvotes: 0
Reputation: 8022
Yes it is possible to import the class with the import statement. For better understanding let's assume that you have three folders foldera
, folderb
and folderc
where foldera
contains a .java file named "ClassA.java
", folderb
contains another .java file named "ClassB.java
" and folderc
contains a .java file named "ClassC.java
". Now, if you want to uses the member data and operations of "ClassA.java
" in "ClassC.java
" you can use the import
statement as shown below:
import foldera.ClassA
If you want to use the member data & operations of "ClassB.java" in "ClassC.java" it is also possible with the import statement
import folderb.ClassB
As per the java source file declaration rule, if the class is a part of a package, the package
statement must be the first line in the source code file, before any import
statements that may be present. In this example, the first line of "ClassC.java"
source file must be package folderc
since it is located in folderc
. Similarly, the first line of "ClassA.java"
source file must be package foldera
, and the first line of "ClassB.java"
source file must be package folderb
.
Hope now you are clear with the concept! Thank you...
Upvotes: 2
Reputation: 2516
yes it is possible just import the package
syntax
import pck.ClassA or import pck.*
Upvotes: 0
Reputation: 1489
Yes it is possible.
If you have the following:
Package: PackA
Class: ClasA
Do:
import PackA.ClassA; //Import the class
OR
import PackA.*; //Import all the classes within the package
Upvotes: 0