Our Recommendation for You Search your Query, You can find easily. for example search by book name or course name or any other which is related to your education

Followers

23: Packages Part 1 Java Programming in Urdu

Java Package

  • java package is a group of similar types of classes, interfaces and sub-packages.
  • Package in java can be categorized in two form, built-in package and user-defined package.
  • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
  • Here, we will have the detailed learning of creating and using user-defined packages.

Example of java package 

The package keyword is used to create a package in java.
Simple example of java package
The package keyword is used to create a package in java.
For run java package program:
javac -d directory javafilename 
forexample
javac -d . Simple.java 
To Compile: javac -d . Simple.java
To Run: java mypack.Simple



How to access package from another package?

There are three ways to access the package from outside the package.

  1. import package.*;
  2.  import package.classname; 
  3. fully qualified name.

Example of package that import the packagename.*


//save by A.java  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello");}  
}  



//save by B.java  
package mypack;  
import pack.*;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}  

Example of package by import package.classname

//save by A.java  
  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
import pack.A;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
} 


Example of package by import fully qualified name

//save by A.java  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
class B{  
  public static void main(String args[]){  
   pack.A obj = new pack.A();//using fully qualified name  
   obj.msg();  
  }  
}