Packages
1. Built-in packages (also known as ready-made package)
Full Example:
Calculator.java
package letmecalculate;
public class Calculator {
public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Demo.java
import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
Full Example: Using fully qualified name
Calculator.java
package letmecalculate;
public class Calculator {
public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Example.java
//Declaring a package
package anotherpackage;
public class Example{
public static void main(String args[]){
//Using fully qualified name instead of import
letmecalculate.Calculator obj = new letmecalculate.Calculator();
System.out.println(obj.add(100, 200));
}
}
- A package as the name suggests is a pack(group) of classes, interfaces and other packages.
- We can use packages to organize our classes and interfaces.
- How to declare packages?
- Syntax:
- package package_name;
- Example:
- package ampics
- How to import packages?
- Syntax:
- import package_name.class_name or *;
- Example:
- import ampics.MCA2;
- import ampics.*;
- MCA2 means only specific class.
- * indicates all members of that ampics.
1. Built-in packages (also known as ready-made package)
- The already defined package like java.io.*, java.lang.* etc are known as built-in packages.
- Example:
- import java.util.Scanner
- Here, java is a top level package
- util is a sub package
- Scanner is a class
- The package we create is called user-defined package.
- Example: ampics.MCA2
- Here, ampics is a package
- MCA2 is a class.
- Reusability: Using packages, you can create such things in form of classes inside a package and whenever you need to perform that same task, just import that package and use the class.
- Better Organization: It is always required to group the similar types of classes in a meaningful package name so that you can organize your project better and when you need something you can quickly locate it and use it, which improves the efficiency.
- Name Conflicts: We can define two classes with the same name in different packages so to avoid name collision, we can use packages.
Full Example:
Calculator.java
package letmecalculate;
public class Calculator {
public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Demo.java
import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
Full Example: Using fully qualified name
Calculator.java
package letmecalculate;
public class Calculator {
public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Example.java
//Declaring a package
package anotherpackage;
public class Example{
public static void main(String args[]){
//Using fully qualified name instead of import
letmecalculate.Calculator obj = new letmecalculate.Calculator();
System.out.println(obj.add(100, 200));
}
}
Tags:
Core java