package packageName;
class demo { public static void main(String args[]) { System.output.println(“Learn Java Really”); } }
package realjava.loops; public class SumN //Class must be public { public void doSum(int n)//method must be bublic { int i=1,sum=0; for(i=1;i<=n;i++) { sum=sum+i; } System.output.println(“Sum=”+sum); } }FactN.java : for factorial of n.
package realjava.loops; public class FactN //Class must be public { public void factorial(int n)//method must be public { int i=1,fact=1; for(i=1;i<=n;i++) { fact=fact*i; } System.output.println(“Factorial=”+fact); } }
package realjava.decision; public class Max2 //Class must be public { public void max2(int a,int b)//method must be bublic { if(a>b) System.output.println(“A is Greater" ); else System.output.println(“B is Greater" ); } }Max3.java : for greatest of 3 numbers.
package realjava.decision; public class Max3 //Class must be public { public void max3(int a,int b,int c)//method must be bublic { if(a>b && a>c) System.output.println(“A is Greatest" ); else if(b>c) System.output.println(“B is Greatest" ); else System.output.println(“C is Greatest" ); } }
import realjava.loops.*; //to import all files import realjava.decision.Max2; //to import Max2.java file only class UsePack { public static vooid main(String args[]) { SumN s=new SumN(); s.doSum(5); FactN f=new FactN(); f.factorial(5); Max2 m2=new Max2(); m2.max2(20,30); //using fully qualified path to use Max3.java realajava.decision.Max3 m3=new realajava.decision.Max3(); m3.max3(10,30,40); } }Compile and interprete the program(UsePack.java) to see the expected output.
package p; class A { class AA { } } class B extends A { }In this example p.A.AA and p.B.AA are fully qualified names but only p.A.AA is canonical name.
import TypeName;Note that, TypeName must be the canonical name of a class or an interface and it must be in existence, for example:
import java.util.ArrayList;The above statement avails class ArrayList only from java.util package in the compilation unit.
import packa.Rect; import packb.Rect;This causes compile time error because of duplicate declarations of Rect type. If the type (class or interface) imported is also declared in the same compilation unit, then the import type is ignored, as shown below:
import packa.Rect class Rect { ----------- ----------- }
Syntax: import PackageName.*;The package name must be the canonical name of the package for example:
import java.io.*;The above statement will avail all the public types to the compilation unit for use.
import packa.*; import packb.Rect;The above statements will not cause compile-time error because class Rect of packb will have higher priority while instantiation.If you write the following declaration statements:
import packa.*; import packb.*;This will cause ambiguity error on instantiation of class Rect in compilation unit code. To resolve ambiguity, full qualifi ed path can be used as follows:
packa.Rect ref = new packa.Rect ();
Syntax: import static PackageName.TypeName.staticMemberName;
Syntax: import static packageName.typeName.*;
Syntax: packageName.TypeName refVar=new packageName.TypeName();But using fully qualified name in code for use types is not a very good programming practice.