Default packages to any class in java

How to import a class from default package in Java?

In Java, classes can be organized into packages for better code organization and reuse. However, sometimes you may encounter a situation where you want to import a class from the default package, which is the package that is used when no package is specified. This can be a bit tricky as there are certain restrictions on accessing classes in the default package from outside of the package. In this article, we’ll look at different methods you can use to import a class from the default package in Java.

Method 1: Using the Fully Qualified Name

To import a class from the default package in Java using the fully qualified name, you need to specify the complete package name along with the class name. Here is an example:

package com.example; public class Main  public static void main(String[] args)  // Using the fully qualified name to import a class from the default package DefaultClass obj = new DefaultClass(); obj.print(); > >

In the above example, we are importing a class named DefaultClass from the default package. To do this, we specify the complete package name along with the class name, like this:

DefaultClass obj = new DefaultClass();

Here, DefaultClass is the name of the class we want to import, and since it is in the default package, we don’t specify any package name before it.

The print() method is just an example method we can call on the DefaultClass object.

Note that importing classes from the default package is generally not recommended, as it can lead to naming conflicts and make your code harder to maintain. It is better to organize your code into packages and import classes from those packages instead.

That’s it! This is how you can import a class from the default package in Java using the fully qualified name.

Method 2: Moving the Class to a Named Package

To import a class from the default package in Java, you need to move the class to a named package. Here are the steps to do it:

  1. Create a new package by right-clicking on the project in the Package Explorer and selecting «New» -> «Package».
  2. Give the package a name, such as «com.example».
  3. Move the class you want to import to the new package. You can do this by dragging and dropping the class file from the default package to the new package in the Package Explorer.
  4. Open the class where you want to use the imported class.
  5. Add an import statement at the top of the class file, specifying the full package name and class name of the imported class. For example:
package com.example; public class Main  public static void main(String[] args)  MyClass myObject = new MyClass(); myObject.myMethod(); > >

In this example, we have imported the «MyClass» class from the «com.example» package and created an object of this class. We then call the «myMethod» method on this object.

That’s it! By moving the class to a named package and importing it, you can now use it in your code.

Method 3: Modifying the Classpath

To import a class from the default package in Java, you can modify the classpath. Here are the steps to do it:

  1. Create a directory named «default» in your project directory.
  2. Move the class file you want to import into the «default» directory.
  3. Open the terminal or command prompt and navigate to the project directory.
  4. Run the following command to set the classpath:
javac -cp . default/ClassName.java

Here is an example code that demonstrates how to import a class from the default package:

// File: Main.java import ClassName; public class Main  public static void main(String[] args)  ClassName obj = new ClassName(); obj.method(); > >
// File: default/ClassName.java public class ClassName  public void method()  System.out.println("Hello, World!"); > >

In the above code, we have created a directory named «default» and moved the «ClassName» class file into it. Then we have set the classpath using the «javac» command and imported the «ClassName» class in the «Main» class. Finally, we have created an object of the «ClassName» class and called its «method» method.

I hope this helps you to import a class from the default package in Java using the classpath.

Источник

Default Package in Java

Default Package in Java

Packages are a mechanism in Java that wraps a group of classes, sub-packages, and different interfaces. They group all related objects like classes, interfaces, etc., and helps in managed ways of providing access and namespaces. The default package is a collection of java classes whose source files do not contain and package declarations. These packages act as the default package for such classes. It provides the ease of creating small applications when the development of any project or application has just begun.

How does the Default Package work in Java?

To work with the package, we should have a package name and directory structure. These two attributes are closely coupled. If the package name is office.employee.cs, then there will be three directories. They will be office, employees, and cs. This structure would be such that cs are present in employees and employees are part of the office. The main directory office would be accessible to the Classpath variable. The packages which contain these classes are supposed to have reverse order of domain name. For example, we can name the packages as an office.employees.cd, office.employees.admin, office.transport.drivers, etc. The packages hence work in a hierarchy. If there is not package defined for a class, then the default package comes into the picture. The package can be assigned to any class which does not have any package defined. There is an unnamed package that does not have any name. The class name is placed into a default package if you do not choose the ‘package’ statement while creating your class definition. Java compiler will automatically take the package name for this class.

Examples to Implement Default Package in Java

Let us see examples that use the default package and see how it works.

Example #1

Explanation: This is the most basic program in Java. Probably the first one you would have ever written when you started learning Java. This program does not have any package mentioned; hence it takes the default package, which is unnamed. It does not throw any error. The compiler chooses the default package, and the code gives the required output. The below snippet shows the output which is expected. It prints the line which says Hello World.

Example #2

package letsdosomemath; public class Calculate < public int add(int a, int b)< return a+b; >public static void main(String args[]) < Calculate cal = new Calculate(); System.out.println("The addition of a and b is " + cal.add(10, 20)); >>

default package in java2

Explanation: The above program uses a user-defined package. The package is declared in this program. The first line declares the package with the name letsdosomemath. A package can always be declared at the beginning of your program before the class begins. Also, a class can have only one package declared. This package is declared and can be used for the programs to follow. The output of this program will be the addition of two integers which are defined. It will call the Calculate class, and the add() function will return the value of the addition of two integers that are passed to this function.

Now this declared package can be used in another program easily.

import letsdosomemath.Calculate; public class Letstry < public static void main(String args[])< Calculate cal = new Calculate(); System.out.println(cal.add(100, 200)); >>

default package in java3

Explanation: We now use the above package in our next program. We use it by importing it explicitly. As the import statement is mentioned, it will not take the default package. We have specified a package to be used, and hence the compiler will go and look for this package. The package specified here is doing the work of adding two integers. Hence we will not have to write the functionality of adding two integers again. The Lottery class will directly create a new object for the Calculate class. The object created is cal. Cal will refer to the package and directly run the function add(). It will return to take the values when the cal.add function is called with integers 100 and 200. The add() function will return the required values, which is 300. Here we did not need to mention the add() function details again. Just by importing the user-defined package we created, we could add the given two integers. We get the desired output which can be seen in the above screenshot.

Example #3

import java.lang.System.*; public class PackageDemo < public static void main(String args[]) < System.out.println("Welcome to EduCBA"); >>

welcome text

Explanation: The above code imports the inbuilt class of java.lang.System. It follows the hierarchy which we have mentioned earlier. The system refers to the functions present in it. The system is a part of sub package lang, which is, in turn, a part of java. The system can be said as a class present in subpackage lang. This package helps us in using the system functions. As we import this package, we are able to use System.out.println. Also, as we have specified a package to be imported, the default package will not be selected in this case. The output of the above code will be as below.

Note: This is a classic example as we are not using any specific functions; the default package consists of the system functions. Hence, if we remove the import statement at the beginning of the code, the program will still work fine. The default package has this functionality.

Conclusion

Hence, the default package is a set of functionalities that provide a basic setup that is needed to run programs. If any specific package is not chosen, the compiler will choose this default package, enabling the Java code to function better. We can also use the built-in packages or create user-defined packages, which can be used as and when required. They will be needed to declared and then imported into the program where it is required. Packages make the code reusable and efficient. Name conflicts can be avoided, and the code is also well organized.

This is a guide to Default Package in Java. Here we discuss an introduction to Default Package in Java, how does it work with respective examples. You can also go through our other related articles to learn more –

89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8

Источник

Читайте также:  Лучшая субд для java
Оцените статью