Forum : package
Brief description  about Online courses   join in Online courses
View Harini  J 's Profile

package

how to create one package inside another package.i.e., i hav created folder of package1,inside package1 i created another package say package2 in package2 i defined some methods under the class class_example.So how to import that particular class class_example which is under package2

here is the question


3. Implement two packages in the following manner:
Package2 should be inside Package1 and we have to define methods in a class called class_example in package2. This class is required to be imported in our main class using the below import statement:
import package1.package2.class_example;

I have coded in following manner in this flow

D:\Java Project Modules\package
package1 -->package2-->class_example.java which is like this

package package2;

public class Class_example
{
public void PrintName ( ) {
System.out.println ("\nMy name is Harini.J!");

}

public void PrintAge ( ) {
System.out.println ("\n My Age is 21 year Old");

}

}
so how to import this particular class only.I am getting error like file does not exist.

main method looks like this which i hav coded

import package1.package2.Class_example;

public class Myexample
{
public static void main(String[] args)
{
Class_example ce=new Class_example();
ce.PrintName();
ce.PrintAge();
}
}
please kindly help me out
Asked by Harini J | Jun 28, 2010 |  Reply now
Replies (4)
View avinash pagunta 's Profile
thanks bikram ..
Aug 22, 2010
View harini j 's Profile
thank you so much for ur reply.It is working
Jun 30, 2010
View bikram-ballav the-indian-student 's Profile
I think i can help you. First create a folder name package1 then create another folder inside package1 and name it package2. Inside package2 create a java file name class_example, declare all ur necessary methods on class_example. And define package in class_example as package package1.package2;
Now when u import the package in main class write like this
import package1.package2.class_example;
Jun 29, 2010
View arun kumar das 's Profile
Please make sure you are using the correct path. I think the problem here is that you have created a folder named package and used the name package1 in your program.
Jun 29, 2010