Create packages

Java Packages :

  • behaves like a namespace with hierarchies where "." delimits a folder

A)HelloWrold.java

package myjava.world;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
 

B)MathTesting.java

package world;
public class MathTesting
{

public int method(int n1)
{
return (n1+10);
}

}

 

C)TestPack.java

import world.*;
public class TestPack
{
public static void main(String[] args)
{
MathTesting mt = new MathTesting();

System.out.println("Got it from world Test Pack\t"+ mt.method(12));
}

}
 

Step: 1 Create these files as shown in the illustrations

Step: 2

compile C:\myjava\world>javac MathTesting.java ;

If we compile, as shown in the illustration below, it will compile but there will be a runtime error due package setup

Set Class path as : C:\>set CLASSPATH=.;C:\;

Now testing another