menu
Explain about Java Packages and constructors?
Explain about Java Packages and constructors?
A list of classes, sub-packages, and interfaces is PACKAGE in Java. It helps organize the classes into a framework for the folder and makes it easy to find and use them. More importantly, it helps increase the reusability of code. Java package has a unique name and organizes its classes and interfaces into a different namespace, or group of names.

Explain about Java Packages and constructors?

A list of classes, sub-packages, and interfaces is PACKAGE in Java. It helps organize the classes into a framework for the folder and makes it easy to find and use them. More importantly, it helps increase the reusability of code. Java package has a unique name and organizes its classes and interfaces into a different namespace, or group of names.

Although it is not possible for interfaces and classes of the same name to appear in the same package, they may appear in various packages. This is possible by assigning each Java training package a separate namespace.

Syntax:-

NameOfPackage package;

Let's research with an example kit. We describe a class and object and compile it later on in our p1 package. We execute the code as a Java package after compilation.

How can a Java package be created?

It is an easy task to build a package as follows:

Select the name of the package.

Include the package command in your Java Source File as the first line of code.

The Source file contains the classes you want to use in the kit, interfaces, etc.

Compile to build packages for Java package

Step 1)

Consider, in Java, the following package program:

The p1 package;

Class c1(){

M1(){ public void m1(){

System.out.println('c1's m1');

}

Static public void principal(string args[]){

C1 obj = fresh c1();

M1(); obj.m1();

}

}

Here,

To bring a class into a java package, describe package p1 on the first line of code.

Build a c1 class

Definition of an m1 method that prints a line.

Defining the principal form

Creating a Class c1 object

Method of Calling m1

Step 2)

Save this file as demo.java in the next step.

Step 3)

We compile the file in this step.

The compilation is done. There is a class file c1 made. Has no java online course package been produced, however? The solution to the next move is

Step 4)

Now we need to create a Java package and use the command to create it.

Javak-d. Demo.java.java

This command forces the development of a Java package by the compiler.

The current working directory is defined by the "." operator.

Step 5)

A Java package p1 is created when you execute the code. You can see the c1.class file when you open the java package p1 on the inside.

Step 6)

Using the following code, compile the same file

Javak-d .. Demo.java.java

The parent directory is indicated here by "..". In our case, the file will be saved in the C Drive parent directory.

When the above code is run, the file is stored in the parent directory.

Step 7)

Now let's assume that inside our current java package p1, you want to build a sub package p2. We will then change our code as

The p1.p2 package;

The c1{ class

M1() public void {{

System.out.println('c1's m1');

}

}

Stage 8)

Compilation of a file

As shown in the screenshot below, a p2 sub-package is generated with class c1 within the package.

Step 9)

To run the code, mention a fully qualified class name, i.e. the name of the Java package followed by the name of the sub-package followed by the name of the class.

P1.p2.c11 Java

"m1 of c1"m1 of c1.

How to import Java Package

You have to use its completely qualified name to build the object of a class (bundled in a package) in your code.

EXAMPLE:

Object java.awt.event.actionListner = new object java.awt.event.actionListner();;

But for any class you want to use, it could become tedious to type the long dot-separated package pathname. It is suggested, instead, that you use the import statement for more core java online training.