Introduction (5 minutes)
Definition of package (15 minutes)
Syntax with example (10 minutes)
Example (10 minutes)
Assessment (10 minutes)
A 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.
The package keyword is used to create a package in java.
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
If you are not using any IDE, you need to follow the syntax given below:
javac -d directory javafilename
For example
javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
Output:Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.
e:\sources> javac -d c:\classes Simple.java
To run this program from e:\source directory, you need to set classpath of the directory where the class file resides.
e:\sources> set classpath=c:\classes;.;
e:\sources> java mypack.Simple
The -classpath switch can be used with javac and java tool.
To run this program from e:\source directory, you can use -classpath switch of java that tells where to look for class file. For example:
e:\sources> java -classpath c:\classes mypack.Simple
Output:Welcome to package
What is the purpose of a package in Java?
A) To group related classes and interfaces together
B) To increase the size of the program
C) To import classes from other packages
D) To create variables
Which keyword is used to declare a package in Java?
A) import
B) package
C) class
D) public
Which of the following is the correct syntax to import a package in Java?
A) import package_name.*;
B) import package_name;
C) import *package_name;
D) import package_name.class_name;
Which of the following is the default package in Java if no package is declared?
A) java.utils
B) java.lang
C) default
D) java
Which statement is true about packages in Java?
A) A package can only contain classes
B) A package is used to store only interfaces
C) Packages help avoid name conflicts and organize code
D) Packages can only be used for public classes
If a class is in a package called com.example, how would you access it from another class in a different package?
A) com.example.ClassName
B) import com.example.ClassName
C) import com.example.*
D) All of the above
Which of the following types of access modifiers can be used for a package in Java?
A) public
B) private
C) protected
D) None of the above
A) To group related classes and interfaces together
B) package
A) import package_name.*;
B) java.lang
C) Packages help avoid name conflicts and organize code
D) All of the above
D) None of the above