How to call a class from another class in Java?

Calling a class from another class: Here, we are going to learn how to call a class from another class in Java programming language? By Preeti Jain Last updated : March 23, 2024

In Java, we can call a class from another class. There are two ways to access a class from another class,

  1. With the help of Fully Qualified Name
  2. With the help of Relative Path

1) With the help of Fully Qualified Name

Here, we will access a class from another class by using Fully Qualified Name.

If we want to access a class in another class of different package, then, we use Fully Qualified Name and the syntax is,

Syntax

package_name.classname;

Example to call a class from another class using fully qualified name

For example, we want to access ArrayList of java.util package in MyClass, here, we don't need to import the ArrayList class in MyClass class.

// accessing class in another class by using
// Fully Qualified Name

public class MyClass {
    public static void main(String[] args) {
        // Creating an instance of ArrayList by using
        // Fully Quaified Name
        java.util.ArrayList al = new java.util.ArrayList();

        // By using add() method to add few elements
        // in ArrayList
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
        al.add(50);

        // Display ArrayList
        System.out.println("ArrayList :" + al);
    }
}

Output

ArrayList :[10, 20, 30, 40, 50]

If we don't use Fully Qualified Name then in that case, we should import the package containing that class which we want to use in the class where we want.

Syntax

import package_name.*;

If we want to access ArrayList in MyClass, then, we need to import the package where the ArrayList class is defined in MyClass class.

Example to call a class from another class by importing the package

// Java program to demonstrate the example of 
// accessing class in another class by using
// Sub Qualified Name

import java.util.*;

public class MyClass {
    public static void main(String[] args) {
        // Creating an instance of ArrayList without using
        //Fully Qualified Name like java.util.ArrayList
        ArrayList al = new ArrayList();

        // By using add() method to add few elements
        // in ArrayList
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
        al.add(50);

        // Display ArrayList
        System.out.println("ArrayList :" + al);
    }
}

Output

ArrayList :[10, 20, 30, 40, 50]

2) With the help of Relative Path

Here, we will access a class in another class by using Relative Path.

Instead of using Fully Qualified Name, we can use the Relative Path of the class that is associated with the package containing that class.

Example to call a class from another class using relative path

// Java program to demonstrate the example of 
// accessing java file in another class by using
// Relative Path

import java.io.*;

public class MyClass1 {
    public static void main(String[] args) {
        // Accessing a file by using relative path
        File file = new File("\\Programs\\file_1.doc");

        // Display File Name
        System.out.println("File Name:" + file.getName());
    }
}

Output

File Name:\Programs\file_1.doc



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.