Tag: package

Packages in Java, Usage of Packages – Part 2 of Parts 2

java-logo

Naming conventions:

Companies use their reversed Internet domain name in their package names, like this:

com.company.packageName

How to Use a Package:

Calling it by its full name:

For example,

Com.package1.package2 myNewClass = new com.package1.package2();

This type of calling is very time consuming as it is long. It is recommended to use the second method.

Calling with import:

Use “import” keyword to access packages. In program when you write import com.package1.package2, then every time we can type only “package2″, then compiler can understand that we are calling it as com.package1.package2.
Example illustration is best to understand:

import com.package1.package2;
class PackageDemo {
package2 myNewClass= new package2 ();



}

Ways of importing a package:

We can import a package in two ways in to our program when we are coding.
That is, One way is to Package Importing only a single member.
//here ‘subclass’ is a java file in myPackage2

import com.package1.package2.childClass;
class PackageDemo {
childClass myNewClass= new childClass();



}

Second way is, Importing all members of a package.
import com.package1.*;
import java.sql.* ;
When we use *, only the classes in the package referred are imported, and not the classes in the sub package.
For every execution of the Java program, JRE automatically imports two entire packages by default:

The java.lang package java library and the current user defined package by default.

Points to remember:

Sometimes class name conflict may occur. For example:

There are two packages package1 and package2. Assume that both of these packages contains a class “PackageDemo” with the same name, let it be PackageDemo.java. Now both this packages are imported by some other class.

import package1.*;

import package2.*;

The above statements will cause compilation error, because compiler will be in confusion bcz there are two classes with same in two different packages which are imported in the current program. To avoid these naming conflicts in such a situation, we have to be more specific and use the member’s qualified name to indicate exactly which PackageDemo.java class we want:

package1.PackageDemo myNewClass1 = new package1.PackageDemo();

Package2.PackageDemo myNewClass2 = new package2.PackageDemo();

While creating a package, which needs some other packages to be imported, the package statement should be the first statement of the program, followed by the import statement.

Compiling package:

The java compiler can place the byte codes in a directory that corresponds to the package declaration of the compilation unit. The java byte code for all the classes(and interfaces) specified in the source files PackageDemo1.java and PackageDemo2.java will be placed in the directory named package1/package2 , as these sources have the following package declaration

package  package1.package2;

The absolute path of the package1/package2 directory is specified by using the –d (destination directory) option when compiling with the javac compiler.

Assume that the current directory is /packages/project and all the source files are to be found here,the command,

javac –d .file1.java file2.java

Issued in the working directory will create  ./ package1/package2(and any sub directories required) under the current directory, and place the java byte code for all the classes(and interfaces) in the directories corresponding to the package names. The dot (.) after the –d option denotes the current directory. Without the –d option, the default behaviors of  the java compiler is to place all the class files in the current directory rather than the appropriate sub directories.

 

Run the Program with Packages:

Since the current directory is /packages/project and we want to run file1.java,the fully qualified name of the file1 class must be specified in the java command,

java package1.package2.file1

Classpath :

It is a environmental variable, which contains the path for the default-working directory (.).

The specific location that java compiler will consider, as the root of any package hierarchy is, controlled by Classpath

Compile java 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 run java package program

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.

Advertisements