15-Packages-in-Java
15-Packages-in-Java
1
▪ This package is able to provide predefined classes JColorChooser, JFileChooser,......
and interfaces representing all GUI components Ex-6 : java.net:
in order to prepare GUI applications. ▪ If we prepare any java application without using
▪ This package has provided the following classes Client-Server Arch then that java application is
and interfaces to prepare GUI applications. called as Standalone Application.
Ex : Component, Label, TextField, ▪ If we prepare any java application on the basis
TextArea, Button, CheckBox, of Client-Server Arch then that java application
List, Choice, Frame,..... is called as Distributed application.
Ex-5 : javax.swing: ▪ To prepare distributed applications, JAVA has
▪ This package is able to provide predefined library provided the following distributed technologies.
to prepare GUI applications. Ex : Socket Programming
FAQ:- What are the differences between AWT RMI [Remote Method Invocation]
[java.awt] and SWING [javax.swing]? CORBA [Common Object Request Broker
1. AWT provided GUI components are platform Arch/Agent]
dependent GUI components. EJBs [Enterprise Java Beans]
SWING provided GUI components are Platform Web Services
Independent GUI Components. ▪ java.net package has provided predefined library
2. AWT provided GUI components are heavy weight to prepare distributed applications by using
GUI components. Socket Programming.
SWING GUI components are light weight GUI Ex : Socket
components. ServerSocket
3. AWT provided GUI Components are basic GUI URL, URI, URN
components. URLConnection
Ex : Ex-7 : java.rmi:
TextField, TextArea, Label, Button,.... ▪ java.rmi package is able to provide predefined
SWING provided GUI components are most library to prepare Distributed applications on the
advanced GUI components. basis RMI.
EX: JColorChooser, JFileChooser, JTable, JTree,.... Ex : Remote
predefined classes and interfaces to design GUI ▪ To prepare JDBC applications, java has provided
JComponent, JTextField, JPasswordField, ▪ java.sql package has provided the following classes
JButton, JCheckBox, JRadioButton, JFrame, and interfaces to prepare JDBC applications.
2
Ex : Driver, DriverManager, Connection, public class Test{
Statement, PreparedStatement, public static void main(String[] args){
CallableStatement, ResultSet, System.out.println(“Package Demo”);
ResultSetMetaData, DatabaseMetaData, .... }
}
2. User defined Packages :-
▪ These packages are defined by the developers as Compilation Command:-
per their application requirements. 1.
Syntax : package package_Name; javac Test.java
▪ If we want to use package declaration statement Note: Generated Test.class file will be placed in
in java applications then we have to use the current working directory.
following two conditions. 2.
a. Package Declaration Statement must be first javac –d . Test.java
statement. Note: Here, .d is used as destination to place
b. Package name must be unique, it must not generated .class files and dot(.) is indicating current
be sharable and it must not be duplicated. working directory.
where package name may be :-
Test.class file will be placed in :-
1) directly a single name
2) sub package names with .(dot) operator
Ex : package p1;
package p1.p2.p3;
▪ If we want to use package declaration statement
in java files then we have to
Note:- Type javac in command prompt and you will
use the following two condition:
find:-
1) Package declaration statement must be the first
statement in java file after the comment section.
2) Package name must be unique, it must not be
sharable and it must not be duplicated.
There is one universally excepted naming conversion
for packages i.e. to use internet domain name in If the corresponding package structure not already
reverse. available than this command itself will create
Ex : corresponding package structure.
As destination instead of dot(.), we can take any
valid directory name.
Ex : javac –d f: Test.java
3
If the specified directory is not already available than 2. import statement (Any Number (including
we will get compile time error. zero))
Ex : javac –d z: Test.java 3. class/ interface/ enum declaration (Any number
Note: If z: not available than we will get CTE saying (zero also))
– directory not found z: All of the following are valid:
How to execute package program:
▪ At the time of execution we have to use fully
qualified name.
Ex : java com.myproject.mycourse.Test Note:- An empty source file is a valid java program
Output:- Package Demo hence the following are valid java source files.
Ex-1 :
Conclusion 1 :-
Just save a blank file as Test.java. It will compile
In any java source file there can be at-most one
successfully, but will not be execute.
package statement i.e. more than one package
Ex-2 : package pack1;
statement is not allowed otherwise we will get
Just save above file as Test.java. It will compile
compile time error.
successfully, but will not be executed.
Ex : package pack1;
Ex-3 : import java.util.*;
package pack2; Just save above file as Test.java. It will compile
public class A{ successfully, but will not be executed.
} Ex-4 : package pack1;
Output:- Compile time error. import java.util.*;
D:\Java>javac A.java Just save above file as Test.java. It will compile
A.java:2: class, interface, or enum expected successfully, but will not be executed.
package pack2; Ex-5 : class Test { }
Conclusion 2 :- Just save above file as Test.java. It will compile
In any java program the first non-comment successfully, but will not be executed.
statement should be package statement (If it is FAQ:- Is It Possible To Provide More Than One
available) otherwise we will get compile time error. Package Declaration Statement Within A Single Java
Ex : import java.util.*; File?
package pack2; ▪ No, it is not possible to provide more than one
public class A{ package declaration statement within a single
} java file, because, package declaration statement
Output:- Compile time error. must be provided as first statement in java files.
D:\Java>javac A.java ▪ To provide package names in java applications,
A. java:2: class, interface, or enum expected JAVA has provided a convention like to include
package pack1; our company domain name in reverse.
4
package p3; // Invalid java.util.ArrayList
Ex: real time package declaration using naming ▪ A Java program with import statement:
conventions. import java.io.*;
package com.fliqi.icici.transactions.deposit; BufferedReader br = new BufferedReader(new
com.fliqi---> Company Domain name in InputStreamReader( System.in));
reverse. ▪ A Java program without import statement:
icici -----> client/project name java.io.BufferedReader br = new
transactions---> Module name java.io.BufferedReader(new
deposit ------> sub module name java.io.InputStreamReader(System.in));
▪ If we declare classes and interfaces in a package
Application-1 :
and if we want to use these classes and
D:\abc
interfaces in the present java file then we have
Employee.java
to import the respective package to the present
C:\xyz
java file.
com
▪ To import packages to the present java file we
|----fliqi
have to use the following Syntax: -
|----core
▪ import package_Name.*;
|-----Employee.class
▪ It able to import all the classes and interfaces
D:\javaapps\ packages
of the specified package.
Test.java
Ex : import java.util.*;
Test.class
import package_Name.member_Name;
Employee.java :
▪ It able to import only the specified member from
package com.fliqi.core;
the specified package.
public class Employee{
Ex : import java.util.ArrayList;
String eid;
Note: In java files, we are able to provide at most
String ename;
one package declaration statement, but we are able
float esal;
to provide any number of import statements.
String eaddr;
FAQ:- Is It Possible To Use Classes And Interfaces
public Employee(String eid, String
of A Particular Package without Importing the
ename, float esal, String eaddr){
Respective Package?
this.eid=eid;
▪ Yes, it is possible to use classes and interfaces
this.ename=ename;
of a particular package without importing that
this.esal=esal;
package, but by using fully qualified names of
this.eaddr=eaddr;
the respective classes and interfaces.
}
Note: Specifying class name or interface name along
public void getEmpDetails(){
with package name is called as "Fully Qualified System.out.println("Employee Details");
Name". System.out.println("-------------------
Ex : java.io.BufferedReader ----");
5
System.out.println("Employee Id :"+eid);
System.out.println("Employee Name
:"+ename);
System.out.println("Employee Salary
:"+esal);
System.out.println("Employee
Address:"+eaddr);
}
}
On Command Prompt :-
D:\abc>javac -d C:\xyz Employee.java
Test.java
import com.fliqi.core.*;
public class Test{
public static void main(String[] args){
Employee emp=new Employee("E-
111", "Dinesh", 50000.0f, "Gurugram");
emp.getEmpDetails();
}
}
On Command Prompt :-
D:\javaapps\packages>set
classpath=C:\xyz;.;
D:\javaapps\packages>javac Test.java
D:\javaapps\packages>java Test
--- Employee Details----
Classpath : To specify the location where package
is existed.
import com.fliqi.core.* : To specify in which package
Employee class is existed.