Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Package

• In Java a Package is a simple folder which automatically gets generated after the compilation of source codes.
• Package is used to avoid conflicts in naming of classes.
• In Java, if you are not using any package explicitly then your current working directory will be considered as a package by default

            
class JTC{
	public static void main(String[] args) {
		Hello h1=new Hello();
		Hai hai=new Hai();
		h1.m1();
		hai.show();
	}
}
class Hello{
	void m1(){
 		System.out.println("m1 in Hello");
 	}
}
class Hai{
	void show(){
		System.out.println("show in Hai");
	}
}
    m1 in Hello
    show in Hai
            

In this example we can see that we have a simple code and we have saved this source code in a folder JTC. As you can see that in the above program there is no compilation error and so after successful compilation of the source codes the three .class files - Hello.class, Hao.class and JTC.class - get placed by default in the same folder JTC where we have saved our source codes.

• In Java we can create our own package as per the requirement.

Syntax: - package <package_name>;

Here, package is a Java keyword, so it must be lower case. An important point that we need to remember while working with package is that package configuration must be the first statement inside the program.

• When we write a Java program with package feature then we need to compile the program along with a –d; -d is an option in the Javac compiler that is used for creating a directory structure.

Example: - javac –d . Test.java

Here, in this example javac is a command to compile the source code; -d is dedicated for creating the directory structure; in this manner the folder structure gets created in the current working directory.

Example: -javac –d d:\ Test.java

Here, in this example javac is a command to compile the source code; -d is dedicated to create directory structure; in this manner the folder structure gets created in specific directory- like as shown in this example, the folder structure gets created in D drive.

Note:In order to run Java program with package configuration we must have to use fully qualified name.

Example: - java com.P1.P2.JTC

            
package com.P1.P2;
class JTC{
	public static void main(String[] args) {
		Hello h1=new Hello();
		Hai hai=new Hai();
		h1.m1();
		hai.show();
	}
}
class Hello{
	void m1(){
 		System.out.println("m1 in Hello");
 	}
}
class Hai{
	void show(){
		System.out.println("show in Hai");
	}
}
    m1 in Hello
    show in Hai
            

In the above example we have configured a package com.P1.P2; so after the compilation of source codes, automatically a folder structure com -> P1 -> P2 gets created. The first folder is com which contains one folder P1 and finally inside the P1 folder we have P2 folder and all the .class files, i.e. Hello.class, Hai.class and JTC.class are placed in P2 folder.

• In Java we cannot directly access a .class file of a package from outside the package. In order to access a .class file of a package we need to import first then only we can access that file, if we attempt to access the file directly we will get an error at compile time.

            
package com.P1;

public class A{
	public void m1(){
		System.out.println("m1 in A class of com.P1 package");
	}
}

As we can see in the above program, we have a class A inside com.P1 package and it contains an instance method void m1().

            
package com.P2;

public class JTC{
	public static void main(String arg[]){
		// A a1 = new A ();:- error 
		com.P1.A a1 = new com.P1.A (); 
		a1.m1();
	}
}
        
    m1 in A class of com.P1 package
            

In this section we have a class JTC having main method and in main method first we are creating an object of class A directly and because class A is available in com.P1 package and class JTC is available in com.P2 package, we get an error that will be displayed as- cannot find the symbols- at compile time. In the next section of the example we can see that we are using the complete name of class A which is inside com.P1 package.

We have an alternate way called import concept to execute the above process, this is shown as below:

As per the requirement we can import a .class file in the following manner:

            
package com.P1;

public class A{
	public void m1(){
		System.out.println("m1 in A class of com.P1 package");
	}
}
        

As we can see in the above program we have a class A inside com.P1 package and it contains an instance method void m1().

            
package com.P2;
import comP1.A;

public class JTC{
	public static void main(String arg[]){
		A a1 = new A (); 
		A1.m1();
	}
}
        
    m1 in A class of com.P1 package
            

Here, we can see we are importing A. class file using import keyword; now we can easily create an object of A class directly; in the final step we are invoking m1() method which is an Instance method of com.P1.A class.

• In Java we can also import a package. When we import a package the entire .class file which is placed inside that package imports at the same time.

            
package com.P1;

public class A{
	public void m1(){
		System.out.println("m1 in A class of com.P1 package");
	}
}
        
            
package com.P1;

public class B{
	public void m2(){
		System.out.println("m2 in B class of com.P1 package");
	}
}
        
            
package com.P1;

public class C{
	public void m3(){
		System.out.println("m3 in C class of com.P1 package");
	}
}
        

In the above example we have three different source codes and after their successful compilation we get A.class, B.class and C.class files in the same com.P1 package.

            
package com.P2;

/*import com.P1.A;
import com.P1.B;
import com.P2.C;*/

import com.P1.*;

public class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		A1.m1();
		B b1 = new B();
		B1.m2();
		C c1 = new C();
		C1.m3();
    }
}
        
    m1 in A class of com.P1 package
    m2 in B class of com.P1 package
    m3 in C class of com.P1 package
            

Here, in this section we have a JTC class which is in com.P2 package and here we are creating objects of A, B and C classes and invoking their methods. Since A, B and C classes are available in com.P1 package so the first option is that we can import A,B and C classes one by one or we can import P1 package directly, here com.P1.* represents all the .class files available in com.P1 package.

• In Java, in package import feature, we can directly import static member only of a class.

            
package com.P1;

public class A{
	public static int a = 10;

	public int b = 20;

	public static void m1(){
		System.out.println("m1() in A class of com.P1 package");
	}
	
	public void m2(){
		System.out.println("m2() in A class of com.P1 package");
	}
}

Here, in this example we have a class A available in com.P1 package that contains instance members- public int b = 20 and public void m2(), and static members - public static int a = 10 and public void m1().

            
package com.P2;

import static com.P1.A.a; // importing static int a = 10
import static com.P1.A.m1; // importing static void m2(){}

// import static com.P1.A.m2; 
// import static com.P1.A.m1;

public class JTC{
	public static void main(String arg[]){
		System.out.println("a :- "+a);
		m1();
	}
}
                10 :- m1() in A class of com.P1 package
            

In this section we can see that we are using static import concept to directly import static int a = 10 and static void m1 () and in the main method we are directly accessing all the static members.