Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have one doubt regarding Java.
I have 2 packages in java. In that i have imported one package into another
as follows.


Java
package package1;
public class Base1{
public void Method1(){
System.out.println("Method1");
}
}

package package2;
import package1;
public class Base2{
public void Method2(){
System.out.println("Method2");
Base1 b1=new Base1();
b1.Method1();
}
}


import package2;
public class clsMain{
public static void main(String[] args){
Base2 b2=new Base2();
b2.Method2();
}
}




Can any body tell me this
its showing some errors as follows
while compiling the second file Base2 the following error is coming

Base2.java:2: '.' expect
import Package1;


and While compiling the clsMain file its showing following errors

clsMain.java:1: package package2 does not exist
import package.*;
^
.\Base21.java:2: '.' expected
import Package1;
             ^
Use.java:9: cannot access Base2
bad class file: .\Base2.java
file does not contain class Base2
Please remove or make sure it appears in
th.
                Base2 m = new Base2();


If anything wrong is there in this can you give code for this scenario please
Posted
Updated 2-Jan-12 9:38am
v2

You don't import packages. You import classes in a package.

So the form is one of the following (brackets used to indicate a name not syntax.)
Java
import {package}.*;
import {package}.{class name in package};

So you would use
Java
import Package1.*;

Also note that for future questions it helps to specify the files in which your code is in.
 
Share this answer
 
v2
There are 2 ways in order to use the public classes stored in package.
a) Declare the fully-qualified class name
...
Java
world.HelloWorld helloWorld = new world.HelloWorld();
world.moon.HelloMoon helloMoon = new world.moon.HelloMoon();
String holeName = helloMoon.getHoleName();


b) Use an "import" keyword
Java
import world.*;  // we can call any public classes inside the world package
import world.moon.*;  // we can call any public classes inside the world.moon package
import java.util.*;  // import all public classes from java.util package


Reference link :-
http://www.jarticles.com/package/package_eng.html[^]
Using Package Members[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900