Skip to main content

Packages in Java

Introduction

Package is a java directory used to group related classes and interfaces and used to separate new classes from existed classes. Means if we use packages we can create multiple classes with same name.

How to Create package?

Nothing fancies, you just need to follow below syntax:
 package  "package-name";
 e.g. package com.app;
 

Rule:

It should be First statement in Java Program
According to industry standard, every class file or interface file first line should be package statement. And it starts from reverse domain of the company e.g. com.mycompany.functionality.

Program showing creation of class with package :

 //Example.java
 package com.app;
 class Example {
  public static void main(String args[]){
          System.out.println("Creating package!!!");
  }
 }
 

Execution:

Go to directory where you have created class file
>Javac -d . Program.java
>Java com.app.Example.java

Why to use -d ?

Packaged classes must be compiled with -d option , else we cannot execute the compiled .class file as package folder will not be created without -d option. -d stands for creating directory.
Compiler replaces the class name with packagename.classname and also constructor name with packagename.classname.
·         Internally in compilation phase, class name is converted from Example to com.app.Example as class is associated with com.app package.
·         Since the class name is com.app.Example, while executing  JVM search for a folder with name “com” and inside com it search for “app”. If not found, class execution stopped with java.lang.NoClassDefFoundError . So we must use -d option while creating package to create folder that will associate with the class.

Why to use "." ?

·         To create compiled  package folder with .class file in same directory
Why java com.app.Example why not java Example?
·         As we already seen the compiled classname is having package name associated with classname.
·         So when you will try to run class file without package name, it will give java.lang.NoClassDefFoundError error.


Can we create classes with predefined  class name?

Yes. We can create user defined classes or custom classes with predefined class name.

Then how can we differentiate between these two classes ?

·         By using package name.
·         If there is any class defined locally with same predefined class name, we must access it with its package name. Else it will be accessed from current working directory if classpath environment variable is setup with “.” Operator.
·         A class must be access with fully qualified name to use its members from another package.
·         If there is a class with name String in your current working directory, your program will compile but will not be executed as JVM consider main method parameter is current local String class not predefined class.
·         To solve this problem, we must access main method parameter with package name java.lang

//String.java
-----------------------------------------------------------------------
package com.app;

public class String {
                public static void main(java.lang.String[] args) {
                                java.lang.String str = "Hello World";
                                System.out.println(str);
                }
}
-----------------------------------------------------------------------
Thank You..!!!

Comments