Connecting packages in java

Previous
Next

Connecting same package classes:

  • Package is a collection of classes.
  • We can connect the classes by accessing one class members from another class.
  • Default access permissions are package level in java. Hence we can connect them directly.

First.java:

package nit;
class First 
{
	static void m1(){
		System.out.println("nit.First class m1()");
	}
	void m2(){
		System.out.println("nit.First class m2()");
	}
}

Second.java:

package nit;
class Second 
{
	public static void main(String[] args) 
	{
		System.out.println("nit.Second class main()");
		First.m1();
		First obj = new First();
		obj.m2();
	}
}

Desktop/nit folder contains 2 class files:

We need to run Second class because it contains main() method:

public class: (Connecting classes of different packages)

  • We can access same package classes directly (using default access permissions).
  • To connect classes of different packages, it is mandatory to define the class as public.
  • Public class is visible to all the classes in the application.
  • In real time application development every class recommended to define as public.

Note: If class is public, we need to save the java source file with the same name of class.

We cannot apply private and protected modifiers to class.

  1. private class (Not allowed in Java)
  2. protected class (Not allowed)
  3. <package> class (Can access only within the package)
  4. public class (Can access from outside package also)

Default constructor of Public class:

  • If class is package level, compiler supplies “package level” default constructor only.
  • If class is public, default constructor is also public.
  • “javap” is a command used to check compiler added code to java source file.

Execute following codes using ‘javap’ and check the compiler added constructor modifier.

Code-1:

class Test 
{
	public static void main(String[] args) {
		// logic......
	}
}
public class Test 
{
	public static void main(String[] args) {
		// logic......
	}
}

Can we define two classes with public modifier within the source file?

  • We can define more than one class in java source file.
  • If class is public, file name should be the class name.
  • We cannot define more than 1 public class in Single source file.

Access classes of other package class:

  • We must import classes by connecting packages.
  • We can import classes of other packages only if they are public.
  • We can instantiate the class of other package only if the constructor is public.
  • We can access the members of class (variables or methods) only if they defined as public.

We can import classes of other packages only if they are public.

First.java:

package p1;
class First 
{
	// logic
}

Second.java:

package p2;
import p1.First ;
class Second 
{
	public static void main(String[] args) 
	{
		// logic
	}
}

If the class is public, default constructor will be public; hence we can instantiate the class directly.

First.java:

package p1;
public class First 
{
	// public default constructor will be added here
}

Second.java:

package p2;
import p1.First ;
class Second 
{
	public static void main(String[] args) 
	{
		First obj = new First();
	}
}

Note: We must define variables and methods as public to access from another package class.

First.java

package p1;
public class First 
{
	public static int a=10;
	public static void m1(){
		System.out.println("p1.First class m1()");
	}
	public void m2(){
		System.out.println("p1.First class m2()");
	}
}

Second.java:

package p2;
import p1.First ;
class Second 
{
	public static void main(String[] args) 
	{
		System.out.println("p2.Second class main()");
		First.m1();
		First obj = new First();
		obj.m2();
	}
}

Note: ‘import’ statement creates internal pointer either to class or to package by which class loads at runtime only on use. Unused classes do not load into JVM. As a java programmer we can analyze whether a class is loading or not into JVM by defining a static block. Static block executes implicitly at the time of class loading.

First.java:

package p1 ;
public class First 
{
	static
	{
		System.out.println("p1.First class loading process...");
	}
	public static void test() 
	{
		System.out.println("p1.First class functionality....");
	}
}

Second.java:

package p2;
import p1.First ; /* It doesn't load the class */
class Second 
{
	static
	{
		System.out.println("p2.Second class loading process....");
	}
	public static void main(String[] args) 
	{
		System.out.println("p2.Second class main...");
	}
}

We must access the members of class to load into JVM:

Second.java:

package p2;
import p1.First ; /* It doesn't load the class */
class Second 
{
	static
	{
		System.out.println("p2.Second class loading process....");
	}
	public static void main(String[] args) 
	{
		System.out.println("p2.Second class main...");
		First.test(); // class loads now - execute static block first
	}
}
Previous
Next

Add Comment

Courses Enquiry Form