0% found this document useful (0 votes)
66 views

Types of Java Package

Package is a way to organize Java classes and interfaces by grouping related ones together. Packages avoid naming conflicts and control access. There are built-in Java packages like java.lang and user-defined packages can be created. To create a package, use the package keyword followed by the package name as the first statement in a Java file. The file must be saved in a folder with the same name as the package. Packages can be imported to allow classes in one package to access classes in another.

Uploaded by

.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Types of Java Package

Package is a way to organize Java classes and interfaces by grouping related ones together. Packages avoid naming conflicts and control access. There are built-in Java packages like java.lang and user-defined packages can be created. To create a package, use the package keyword followed by the package name as the first statement in a Java file. The file must be saved in a folder with the same name as the package. Packages can be imported to allow classes in one package to access classes in another.

Uploaded by

.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Package

Package is a collection of related classes. Java uses package to group


related classes, interfaces and sub-packages in any Java project.
We can assume package as a folder or a directory that is used to store similar files. 
In Java, packages are used to avoid name conflicts and to control access of class, interface
and enumeration etc. Using package it becomes easier to locate the related classes and it also
provides a good structure for projects with hundreds of classes and other files.
Lets understand it by a simple example, Suppose, we have some math related classes and
interfaces then to collect them into a simple place, we have to create a package.

Types Of Java Package


Package can be built-in and user-defined, Java provides rich set of built-in packages in form
of API that stores related classes and sub-packages.

 Built-in Package: math, util, lang, i/o etc are the example of built-in packages.
 User-defined-package: Java package created by user to categorize their project's
classes and interface are known as user-defined packages.

How to Create a Package


Creating a package in java is quite easy, simply include a package command followed by
name of the package as the first statement in java source file.
package mypack;

public class employee

String empId;

String name;

The above statement will create a package woth name mypack in the project directory.
Java uses file system directories to store packages. For example the .java file for any class
you define to be part of mypack package must be stored in a directory called mypack.

Additional points about package:

 Package statement must be first statement in the program even before the import
statement.
 A package is always defined as a separate folder having the same name as the
package name.
 Store all the classes in that package folder.
 All classes of the package which we wish to access outside the package must be
declared public.
 All classes within the package must have the package statement as its first line.
 All classes of the package must be compiled before use.

Example of Java packages


Now lets understand package creation by an example, here we created a leanjava package
that stores the FirstProgram class file. 
//save as FirstProgram.java

package learnjava;

public class FirstProgram{

public static void main(String args[]) {

System.out.println("Welcome to package example");

}
}

How to compile Java programs inside packages?


This is just like compiling a normal java program. If you are not using any IDE, you need to
follow the steps given below to successfully compile your packages:
javac -d . FirstProgram.java

The -d switch specifies the destination where to put the generated class file. You can use any
directory name like d:/abc (in case of windows) etc. If you want to keep the package within
the same directory, you can use . (dot).

How to run Java package program?


To run the compiled class that we compiled using above command, we need to specify
package name too. Use the below command to run the class file. 
java learnjava.FirstProgram

Welcome to package example

After running the program, we will get “Welcome to package example” message to the
console. You can tally that with print statement used in the program.

How to import Java Package


To import java package into a class, we need to use java import keyword which is used to
access package and its classes into the java program. 
Use import to access built-in and user-defined packages into your java source file so that your
class can refer to a class that is in another package by directly using its name.
There are 3 different ways to refer to any class that is present in a different package:

1. without import the package


2. import package with specified class
3. import package with all classes

Lets understand each one with the help of example.

Accessing package without import keyword


If you use fully qualified name to import any class into your program, then only that
particular class of the package will be accessible in your program, other classes in the same
package will not be accessible. For this approach, there is no need to use
the import statement. But you will have to use the fully qualified name every time you are
accessing the class or the interface. This is generally used when two packages have classes
with same names. For example: java.util and java.sql packages contain Date class.

Example 
In this example, we are creating a class A in package pack and in another class B, we are
accessing it while creating object of class A.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class {
public static void main(String[] args) {
List<Angajati> angajatiList = new ArrayList<>();
List<String> items = new ArrayList<>();
//String[] items = {};

angajatiList.add(new Angajati("Angajat1", 100, 20, 50));


angajatiList.add(new Angajati("Angajat2", 101, 21, 51));
angajatiList.add(new Angajati("Angajat3", 102, 22, 52));
angajatiList.add(new Angajati("Angajat4", 103, 23, 53));

for (Angajati angajati : angajatiList)


items.add(angajati.getNume());

final JComboBox comboBox = new JComboBox(items.toArray());


JButton firstButton = new JButton("Salveaza nume angajat");
JButton secondButton = new JButton("Salveaza tarif orar");
JButton thirdButton = new JButton("Salveaza ore lucrate");
JButton fourthButton = new JButton("Salveaza avans salariu");
JButton fifthButton = new JButton("Adauga un angajat nou");
JButton sixthButton = new JButton("Afiseaza");
JTextField editText1 = new JTextField("");
JTextField editText2 = new JTextField("");
JTextField editText3 = new JTextField("");
JTextField editText4 = new JTextField("");
JLabel editTextLabel1 = new JLabel("Nume angajat:");
JLabel editTextLabel2 = new JLabel("Tarif orar:");
JLabel editTextLabel3 = new JLabel("Ore lucrate:");
JLabel editTextLabel4 = new JLabel("Avans salariu:");
JLabel editTextLabel5 = new JLabel("-");
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(comboBox);
panel.add(editTextLabel1);
panel.add(editText1);
panel.add(editTextLabel2);
panel.add(editText2);
panel.add(editTextLabel3);
panel.add(editText3);
panel.add(editTextLabel4);
panel.add(editText4);
panel.add(editTextLabel5);
panel.add(firstButton);
panel.add(secondButton);
panel.add(thirdButton);
panel.add(fourthButton);
panel.add(fifthButton);
panel.add(sixthButton);

comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("test");
String selectedItem =
comboBox.getSelectedItem().toString();
Angajati selectedAngajat = null;
for (Angajati angajati : angajatiList)
if (angajati.getNume().equals(selectedItem)) {
selectedAngajat = angajati;
System.out.println("found");
break;
}

if (selectedAngajat != null) {
String nume = selectedAngajat.getNume();
String tarifOrar =
String.valueOf(selectedAngajat.getTarif());
String oreLucrate =
String.valueOf(selectedAngajat.getOre());
String avansSalariu =
String.valueOf(selectedAngajat.getAvans());

editText1.setText(nume);
editText2.setText(tarifOrar);
editText3.setText(oreLucrate);
editText4.setText(avansSalariu);
}
}
});

firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String nume = editText1.getText();
if (nume.trim().length() < 1) nume = "Default";

String selectedItem =
comboBox.getSelectedItem().toString();
Angajati selectedAngajat = null;
int index = 0;
for (Angajati angajati : angajatiList)
if (angajati.getNume().equals(selectedItem)) {
selectedAngajat = angajati;
angajatiList.indexOf(selectedAngajat);
break;
}

selectedAngajat.setNume(nume);
angajatiList.add(index, selectedAngajat);
}
});

secondButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tarifOrar = editText2.getText();
if (tarifOrar.trim().length() < 1) tarifOrar = "10";

String selectedItem =
comboBox.getSelectedItem().toString();
Angajati selectedAngajat = null;
int index = 0;
for (Angajati angajati : angajatiList)
if (angajati.getNume().equals(selectedItem)) {
selectedAngajat = angajati;
angajatiList.indexOf(selectedAngajat);
break;
}

selectedAngajat.setTarif(Double.parseDouble(tarifOrar));
angajatiList.add(index, selectedAngajat);
}
});

thirdButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oreLucrate = editText3.getText();
if (oreLucrate.trim().length() < 1) oreLucrate = "0";

String selectedItem =
comboBox.getSelectedItem().toString();
Angajati selectedAngajat = null;
int index = 0;
for (Angajati angajati : angajatiList)
if (angajati.getNume().equals(selectedItem)) {
selectedAngajat = angajati;
angajatiList.indexOf(selectedAngajat);
break;
}

selectedAngajat.setOre(Double.parseDouble(oreLucrate));
angajatiList.add(index, selectedAngajat);
}
});

fourthButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String avansSalariu = editText4.getText();
if (avansSalariu.trim().length() < 1) avansSalariu = "100";

String selectedItem =
comboBox.getSelectedItem().toString();
Angajati selectedAngajat = null;
int index = 0;
for (Angajati angajati : angajatiList)
if (angajati.getNume().equals(selectedItem)) {
selectedAngajat = angajati;
angajatiList.indexOf(selectedAngajat);
break;
}

selectedAngajat.setAvans(Double.parseDouble(avansSalariu));
angajatiList.add(index, selectedAngajat);
}
});

fifthButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String nume = editText1.getText();
String tarifOrar = editText2.getText();
String oreLucrate = editText3.getText();
String avansSalariu = editText4.getText();
if (nume.trim().length() < 1) nume = "Default";
if (tarifOrar.trim().length() < 1) tarifOrar = "10";
if (oreLucrate.trim().length() < 1) oreLucrate = "0";
if (avansSalariu.trim().length() < 1) avansSalariu = "100";

Angajati angajati = new Angajati(nume,


Double.parseDouble(tarifOrar), Double.parseDouble(oreLucrate),
Double.parseDouble(avansSalariu));
angajatiList.add(angajati);
editText1.setText("");
editText2.setText("");
editText3.setText("");
editText4.setText("");
comboBox.setVisible(false);

panel.validate();
panel.revalidate();
panel.repaint();
}
});

int result = JOptionPane.showConfirmDialog(null, panel, "Test",


JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(comboBox.getSelectedItem());
} else {
System.out.println("Cancelled");
}
}
}

class Angajati {
private String angNume;
private double tarifOrar;
private double oreLucrate;
private double avansSalariu;

public Angajati(String angNume, double tarifOrar, double oreLucrate,


double avansSalariu) {
this.angNume = angNume;
this.tarifOrar = tarifOrar;
this.oreLucrate = oreLucrate;
this.avansSalariu = avansSalariu;
}

public String getNume() {


return angNume;
}

public double getTarif() {


return tarifOrar;
}

public double getOre() {


return oreLucrate;
}

public double getAvans() {


return avansSalariu;
}

public void setNume(String angNume) {


this.angNume = angNume;
}

public void setTarif(double tarifOrar) {


this.tarifOrar = tarifOrar;
}

public void setOre(double oreLucrate) {


this.oreLucrate = oreLucrate;
}

public void setAvans(double avansSalariu) {


this.avansSalariu = avansSalariu;
}

@Override
public String toString() {
return "Angajati{" +
"angNume='" + angNume + '\'' +
", tarifOrar=" + tarifOrar +
", oreLucrate=" + oreLucrate +
", avansSalariu=" + avansSalariu +
'}';
}
}

Hello

Import the Specific Class


Package can have many classes but sometimes we want to access only specific class in our
program in that case, Java allows us to specify class name along with package name. If we
use import packagename.classname statement then only the class with name classname
in the package will be available for use.

Example:
In this example, we created a class Demo stored into pack package and in another class Test,
we are accessing Demo class by importing package name with class name.
//save by Demo.java

package pack;

public class Demo {

public void msg() {

System.out.println("Hello");

}
}

//save by Test.java

package mypack;

import pack.Demo;

class Test {

public static void main(String args[]) {

Demo obj = new Demo();

obj.msg();

Hello

Import all classes of the package


If we use packagename.* statement, then all the classes and interfaces of this package will
be accessible but the classes and interface inside the sub-packages will not be available for
use.
The import keyword is used to make the classes of another package accessible to the current
package.

Example :
In this example, we created a class First in learnjava package that access it in another class
Second by using import keyword.
//save by First.java

package learnjava;

public class First{

public void msg() {

System.out.println("Hello");

}
//save by Second.java

package Java;

import learnjava.*;

class Second {

public static void main(String args[]) {

First obj = new First();

obj.msg();

You might also like