Forum begins after the advertisement:
How to access a Java class in another package
Home › Forums › General Discussion › How to access a Java class in another package
- This topic has 0 replies, 1 voice, and was last updated 1 year, 3 months ago by Terence.
-
AuthorPosts
-
July 28, 2023 at 9:49 pm #11664::
I get a lot of questions regarding how to access classes in different packages when teaching Java programming classes. Hence this forum topic. Alright, let’s get straight to it:
What are packages?
Before we delve into accessing classes across packages, let’s briefly understand what Java packages are. A package is a way of organising related classes and interfaces in Java. It provides a hierarchical structure, and classes within the same package have access to each other without requiring any additional effort.
Every class provided by the Java API resides in some package. The Scanner class, for example, resides in the java.util package, which is why we have to include an import statement in every class where we wish to use a Scanner.
import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { // This is just a simple Java command-line program that // prompts the user to enter their name. Scanner input = new Scanner(System.in); System.out.print("Please enter your name: "); String name = input.nextLine(); // Prints Hello [name]!. System.out.println("Hello " + name + "!"); } }
One thing that is not widely known is this: it is still possible to use the Scanner in the above code without the import statement, if you use the fully-qualified class name of the Scanner class every time you invoke it:
public class MyFirstProgram { public static void main(String[] args) { // This is just a simple Java command-line program that // prompts the user to enter their name. java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Please enter your name: "); String name = input.nextLine(); // Prints Hello [name]!. System.out.println("Hello " + name + "!"); } }
User-defined packages
When you write your own Java classes, it is possible for you to define your own packages. For example:
package MyAccountingProgram; public class Account { private String name; private int id; public Account(String name, int id) { this.name = name; this.id = id; } }
The fully-qualified class name of the Account class is MyAccountingProgram.Account. Hence, whenever I wish to refer to the class, I will either have to include an import statement at the top of any Java file:
import MyAccountingProgram.Account;
…or use the fully-qualified class name to refer to the class every time I want to invoke it:
public class MySecondProgram { public static void main(String[] args) { MyAccountingProgram.Account account = new MyAccountingProgram.Account("John",123); } }
Wrapping Up
Basically, packages exist in Java so that it is possible for different classes to share the same name. This is important because when users write Java code, it is possible for them to define classes that do not belong to a package. If Java did not have packages, it would be impossible for users to define certain classes without a package.
For example, Java has a native
Date
class underjava.lang
. If Java did not have packages, it would not be possible for a user to define aDate
class, because it shares the same name as an existing class. -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: