How to Import Custom Class in Java?
Last Updated :
13 Jun, 2022
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that it tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, etc. In this article, we will discuss how to import custom classes from one project to another project or in the same project.
Custom classes are created by the user for a particular purpose. These classes are not present in the system library of the project. Consider the structure of the project depicted below as follows:

Structure Format:
ProjectName:Main1
|
|
First(package)[ GFG1.java , GFG2.java ] (classes)
|
Second(package)[GFG3.java] (class)
Note: In order to get through the concept, one must go through access modifiers in java to understand the concept of the scope of classes, members, and methods.

Here we will be going through both the structure types as follows which later on will be implementing the same further below:
- Custom class within the same package
- Custom class from another package
Implementation: Importing classes for the same project
Classes in the same project can be imported into any other class in the same project without any import statement in the particular class of the project. Because, by default, all the class members and methods are of default modifier and, according to the scope of the default modifier, they can be accessed in the same package / same package subclass without importing the class. Refer to the above table.
Example 1: GFG1.java
Java
// Package of the class
package First;
public class GFG1 {
int a;
int b;
// Creating constructor
GFG1(int a, int b)
{
this.a = a;
this.b = b;
}
// Function to add the members of the class
int add() { return this.a + this.b; }
}
Example 2: GFG2.java
Java
// Package first
package First;
public class GFG2 {
public static void main(String[] args)
{
// Creating of instance of second class present
// in the same project
GFG1 ob = new GFG1(1, 2);
System.out.println("Addition " + ob.add());
}
}
Output:
Addition 3
Let us do dwell on next where we will be importing custom class from another package.
Implementation:
Classes are imported between the different packages only when particular that need to be imported are marked as public and its member and methods should be of public so that they can be used outside its package.
Example 1: Second package class
Java
package Second;
class GFG3 {
int a;
int b;
// Creating constructor
public GFG3(int a, int b)
{
this.a = a;
this.b = b;
} // Function to subtract the values
public int subtract() { return this.a - this.b }
}
Above second package class is imported to the class of first package
Structure:
Second:(Package)
|
GFG3
First:(Package)
|
GFG2
Example 2: First package class
Java
package First;
// Importing class of second package
import Second.GFG3;
public class GFG2 {
public static void main(String[] args)
{
// Creating reference of the GFG1 class that is
// present in the same project
GFG1 ob = new GFG1(1, 2);
System.out.println("Addition " + ob.add());
// Creating the reference for the GFG3 class that is
// present in the other project
GFG3 ob1 = new GFG3(2, 1);
System.out.println("Subtract " + ob1.subtract());
}
}
Output:
Addition 3
Subtract 1
Similar Reads
How to Create Custom Class in Java?
Class is the collection of objects. Class is not a real-world entity it is just only templates and prototypes or blueprints. Class does not occupy memory. We can write a custom class as per our choice for an illustration purpose a sample is shown in the program below as a helper class. Example: Java
2 min read
How to Execute a .class File in Java?
A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine. Step #1: Compile the .java File Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to
1 min read
Component Class in Java
The Component class is the superclass of all components. A component class can be linked with a page, components of web applications. Component clearly shows that is the graphical representation of an Object.Important methods of Component Class:public void add(Component c): This method inserts a com
9 min read
How to import a class from another file in Python ?
In this article, we will see How to import a class from another file in Python.Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of in
4 min read
How to create a Class in JShell of Java 9
JShell is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and runs from the command line. Jshell have the facility to create a class by which all the efforts can be reduced to write a whol
2 min read
CLASSPATH in Java
Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. Packages are used for: Preventing naming conflicts. For example, there can be two classes with the name Employee in two packages, college.staff.cse.Employee and college.staff.ee.EmployeeMaking searching/l
3 min read
Different Ways to Set a Classpath in Java
Java Virtual Machine(JVM) uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error (java.lang.ClassNotFound
3 min read
Static import in Java
In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.sqrt(), but by using static import
4 min read
How to Call a Method in Java?
In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.What is a Metho
3 min read
How to Add JAR file to Classpath in Java?
JAR is an abbreviation of JAVA Archive. It is used for aggregating multiple files into a single one, and it is present in a ZIP format. It can also be used as an archiving tool but the main intention to use this file for development is that the Java applets and their components(.class files) can be
4 min read