Java Import Keyword
Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class name in code without mentioning the package to which it belongs. To declare all the package's classes, use the asterisk (*).
- Import is a word which describes or includes the packages in the program to make the program to use the specifications of that package.
- The combination of sub packages is also called as packages, interfaces, and classes of a common type.
- In this article we will learn in-depth how to make and use user-defined packages.
Advantages of Java Packages
- Classes and interfaces are categorized using Java packages to make them easier to manage.
- The access control can also be provided by the java package modifier.
- The eliminates naming conflicts can also done using package.
Example of Java Package
From Java programming, a package is implemented with the package name keyword.
public class vedic {
public static void main (String s []){
System.out.println("Welcome to package using import");
}
}
Example:
javac -d. Simple.java
The destination for the created class file is specified by the -d switch. We can use any directory name, like d:/abc (for Windows) or /home (for Linux). Implement if you wish to provide the package in the similar directory (dot).
Running java package program:
Use a fully qualified name, such as mypack. To provide the class simply, etc.
To Compile: javac -d. Simple.java
To Run: java pack. Simple
Output:
Welcome to package using import.
The -d switch, which stands for "destination," instructs the compiler where to save the class file. The current folder is indicated.
Accessing package from another package:
The package of any program can be supported in three ways from outside the package.
- import package. *;
- import package. classname;
- fully qualified name.
Importing packages:
The classes and interfaces ofpackage will be implemented if weimplement thepackage.
Example of package that import the packagename.
package pack.
public class Port {
public void msg () {System.out.println("Hi I am an importer");}
}
//save by Impo.java
package mypack.
import pack. *;
class Impo{
public static void main (String args []) {
Port obj = new Port ();
obj.msg ();
}
}
Output of the given program is:
Hi, I am an importer
Using packagename. Classname
package pack;
public class HH {
public void msg () {System.out.println(“Hi I am package class name");}
}
//save by GG.java
package mypack;
import pack.HH;
class GG {
public static void main (String s []) {
HH obj = new HH ();
obj.msg ();
}
}
Output of the program is given by:
Hi, I am package class name
Using fully qualified name
Only the stated classes of this package will be accessible if you use the fully qualified name. No longer is it necessary to import. However, whenever you visit a class or interface, you must always use the fully qualified name.
When two packages include the same class name, for example when the java. util and java.sql packages do, it is typically utilised. time cla
Example:
//save by Port.java
package act;
public class Port {
public void msg () {System.out.println("I am an implementer of package");}
}
//save by imp.java
package mypack;
class imp {
public static void main (String s []) {
pack.imp obj = new pack. Port () ;//using fully qualified name
obj.msg ();
}
}
Output of the program is:
I am an implementer of package
When you import a package, all its classes and interfaces—with the exception of those from its subpackages—are imported. As a result, you also need to import the subpackage.
Java Sub Packages
The subpackage is a package inside a package. It should be developed to further classify the package.
As an illustration, Sun Microsystem created the java package, which includes numerous classes including System, String, Reader, Writer, Socket, and others. These classes each stand for a certain class, such as Reader and Writer for input/output operations, Socket and ServerSocket for networking, etc.
Example for java package:
package com. contentwriting. core;
class Easy {
public static void main (String s []) {
System.out.println("Hi I am a subpackage");
}
}
To Compile:
javac -d .Easy.java
To Run:
java com. contentwriting. core. Simple
Output of the program:
Hi I am a subpackage
To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates the class by returning that memory. The array object can also be created by using the new keyword.
Java's new keyword creates an instance of a class by allocating the necessary memory for a new object that goes along with it. A reference to that memory is then returned. In Java, the new keyword is frequently used to generate array objects as well. A call to the function Object () {[native code]}, which creates the new object, comes after the new keyword.
In Java, we instantiate a class by allocating the necessary memory for a new object by using the new keyword. After the creation of the object, the new keyword returns a reference to that memory. The Java new keyword can occasionally be used to generate array objects.
Syntax:
Example obj=newExample ();
Important Points to Remember
- New keyword is implemented in the program to create the objects.
- At run time allocation of the memory is done using new keyword.
- The objects that are created, are stored in theheap memory or heap area.
- The java new keyword calls or invokes the object consturctor.
- Calling the function Object () {[native code]} requires a single, postfix parameter.
- The runtime memory allocation for new objects is done via the new keyword.
- The heap memory is used to allocate memory to the new objects.
- A call to the function Object () {[native code]}, which creates the new object, comes after the new keyword.
A call to the class function Object () {[native code]} is the sole postfix parameter required by the new operator. What happens when an object of a class is created is specified in the function Object () {[native code]}. All classes need constructors, which are significant in many important ways. We will use the default function Object () {[native code]} in the example below.
Examples of java New Keyword
Example 1:
It is a straightforward example of creating an object with the new keyword and calling the method with the appropriate object reference.
public class NewEx1 {
void disp ()
{
System.out.println("method is called or invoked");
}
public static void main (String [] s) {
NewEx1 j=new NewEx1();
j. disp ();
}
}
Output:
method is called or invoked
Example 2:
Simple example of calling the function Object () {[native code]} with the appropriate object reference and the new keyword to build an object.
public class NewEx2 {
NewEx2()
{
System.out.println("Constructor is called");
}
public static void main (String [] s) {
NewEx2 o=new NewEx2();
}
}
Output of the program:
Constructor is called
Example 3:
Using the new keyword, we create an object and call the parameterized function Object () {[native code]}.
public class NewEx3 {
int f,g;
NewEx3(int f, intg)
{
this. f=f;
this. g=g;
}
void disp ()
{
System.out.println(f+g);
}
public static void main (String [] s) {
NewEx3 bj=new NewEx3(20,20);
obj. disp();
}
}
Output:
40
Conclusion Points
- Java's new keyword creates an instance of a class by allocating memory for a new object that goes along with it. After that, a reference to that memory is returned.
- This brand-new Java term allocates the RAM during runtime.
- The heap memory is allocated to the objects by the new keyword.
- A single postfix function Object () {[native code]} is needed to create the relevant class when using the new keyword.
- It is possible to think of the crucial term "instantiating a class" as "making an object." An "instance" of the class is what we produce when we construct an object.
- To maximize efficiency, Java primitive types are never implemented as objects but rather as "regular" variables. For help understanding the primitive data types, see Wrapper Classes.
- By using the memory reference that the new keyword returns, we can initialize a variable.
- The memory reference does not need to be assigned to a variable of the right type in order to be utilized to call the members of the new object directly.