Home » Java programming language

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?
Submitted by Preeti Jain, on September 20, 2019

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,

    package_name.classname;

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.

Example:

// 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:

// 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:

// 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.