Skip to content
codingtube

codingtube

Coding and Programming tutorials

  • javascript
  • React
  • ES6
  • React js
  • coding
  • ffmpeg
  • java
  • programming
  • information
  • coding
  • Privacy Policy
  • Twitter trends
  • Age Calculatore
  • Codingtube Community
  • YouTube Tags Generator
  • About
  • Toggle search form

Java Package

Posted on September 3, 2021September 3, 2021 By christo No Comments on Java Package

Packages in java are those class which contains methods without the main method in them.

  • Packages are mainly used to reuse the classes which are already created/used in other program
  • We can define many number of classes in the same package
  • Packages are mainly divided into two types
    • Built in packages
    • User defined Packages

Table of Contents

  • The main use of packages in java
  • Steps for creating package
  • How to Access a package

The main use of packages in java

  • Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Java package provides access protection
  • Java package removes naming collision

Frequently used built in packages are:

  • lang
  • util
  • io
  • awt
  • net
  • applet
  • *These built in packages are placed in the package “java”.
  • In order to use these packages/classes present in the package we need to import them in the current program
  • The syntax for importing packages is: import java.util.*
  • The asterisk symbol tells the compiler to import all the classes in present in the util package.If you want to import specific class then we need to mention the name of the class instead of the asterisk.For eg: import java.util.Scanner

java.lang:

Language supports classes.These are classes that java compiler itself uses and therefore automatically imported.They include classes for primitive types, stirngs, maths, threads and exceptions.

java.util:

Language utility classes such as vectors, hash tables, random numbers, date etc

java.io:

input/output support classes.They provide the facility for input/output of data.

java.awt:

Set of classes for implementing graphical user interface.They include classes for windows, buttons, lists

java.net:

Classes for networking. They include classes for communicating with local computers as well as with internal servers

Create a user defined package

For this we need to create a folder with your desired name say mypackage

package mypackage; 
public class A
{
 public void display()
 {
 System.out.println("Hello world");
 }
}

How to user define package in other programme

Example

import mypackage.*;
class packagedemo
{
 public static void main(String arg[])
 {
 A ob = new A();
 ob.display();
 }
}

Steps for creating package

To create a user defined package the following steps should be involved

  • Declare the package at the beginning of a file using the syntax :-
    • package packageName;
  • Define the class that is to be put in the package & declare it public.
  • Create a subdirectory under the directory where the main source files are stored.
  • Store the listing as the classname. java file in the subdirectory created
  • Compile the file. This create .class file in the subdirectory.
  • Compile the file. This create .class file in the subdirectory
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).

How to compile java package

we 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

Java also supports the concept of package hierarchy. This is done by specifying multiple names in a package statement, seprated by dots (.)

Ex :- package firstPackage.secondPackage;

This approach allows us to group related classes into a package and their group related package into a larger package. Store this package in a subdirectory named firstpackage/secondPackage.

A java package file can have more than one class definition. In such cases, only one of the classes may be declared public & that class name with .java extension is the source file name. When a source file with more than one class definition is compiled, java creates independent .class files for those classes.

How to Access a package

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

  • import package.*;
  • import package.classname;
  • fully qualified name.

If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages

The import keyword is used to make the classes and interface of another package accessible to the current package

Using package name

Example

//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[]){ 
 A obj = new A(); 
 obj.msg(); 
 } 
}
Output:Hello

Using packagename.classname

If you import package.classname then only declared class of this package will be accessible.

Example

import pack.A; 
class B{ 
 public static void main(String args[]){ 
 A obj = new A(); 
 obj.msg(); 
 } 
} 

Using fully qualified name

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class

Example

//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(); 
 }
}

java Tags:import, java, Java Package, packages in java

Post navigation

Previous Post: Classes in java
Next Post: Inheritance in java

Related Posts

Interthread Communication in java java
valueOf( ) Methods in java java
Skyline Algorithm in java Algorithm
Java’s Built-in Exceptions java
Free trees in data structure data structure
The Object Class in java java

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Affiliate Marketing Principles
  • The Basics You Need to Know About Affiliate Marketing
  • Affiliate Marketing Options
  • All About Affiliate Marketing
  • Classification of Database Management Systems
  • Three-Tier and n-Tier Architectures
    for Web Applications
  • Two-Tier Client/Server Architectures for DBMSs
  • Basic Client/Server Architectures in DBMS
  • Centralized DBMSs Architecture in DBMS
  • Tools, Application Environments, and Communications Facilities in DBMS

Categories

  • Affiliate marketing (5)
  • Algorithm (43)
  • amp (3)
  • android (223)
  • Android App (8)
  • Android app review (4)
  • android tutorial (60)
  • Artificial intelligence (61)
  • AWS (3)
  • bitcoin (8)
  • blockchain (1)
  • c (5)
  • c language (105)
  • cloud computing (4)
  • coding (4)
  • coding app (4)
  • complex number (1)
  • Computer Graphics (66)
  • data compression (65)
  • data structure (188)
  • DBMS (44)
  • digital marketing (9)
  • distributed systems (11)
  • ffmpeg (26)
  • game (3)
  • html (6)
  • image processing (35)
  • Inequalities (1)
  • information (4)
  • java (212)
  • java network (1)
  • javascript (9)
  • kotlin (4)
  • leetcode (1)
  • math (21)
  • maven (1)
  • mysql (1)
  • Node.js (8)
  • operating system (109)
  • php (310)
  • Principle Of Mathematical Induction (1)
  • programming (6)
  • Python (4)
  • Python data structure (9)
  • React native (1)
  • React.js (22)
  • Redux (1)
  • seo (4)
  • set (12)
  • trigonometry (6)
  • vue.js (35)
  • XML (3)

sitemap

sitemap of videos

sitemap of webstories

sitemap of website

  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy
  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy

© codingtube.tech