Import a custom class in Java
Asked Answered
F

8

83

How do I import a class I wrote in a different file? All my classes are under the same package.

Fishman answered 23/10, 2011 at 20:36 Comment(1)
What is the actual problem you are having, since you don't have to import classes that are in the same package?Razee
D
108

If all of your classes are in the same package, you shouldn't need to import them.

Simply instantiate the object like so:

CustomObject myObject = new CustomObject();
Doucette answered 23/10, 2011 at 20:40 Comment(1)
If you're aiming simply to call a static method of the class then you probably don't even need to instantiate it as an object - just invoke it as: MyClass.method();Dett
M
39

Import by using the import keyword:

import package.myclass;

But since it's the default package and same, you just create a new instance like:

elf ob = new elf(); //Instance of elf class
Mirabel answered 23/10, 2011 at 20:43 Comment(2)
It wouldn't matter if it wasn't the default package - you don't have to import classes that are in the same package.Razee
@BrianRoach I know. All you need is just an instance. I'm just clearing it up for him.Mirabel
C
7

In the same package you don't need to import the class.

Otherwise, it is very easy. In Eclipse or NetBeans just write the class you want to use and press on Ctrl + Space. The IDE will automatically import the class.

General information:

You can import a class with import keyword after package information:

Example:

package your_package;


import anotherpackage.anotherclass;

public class Your_Class {
    ...
    private Vector variable;
    ...
}

You can instance the class with:

Anotherclass foo = new Anotherclass();
Cowie answered 23/10, 2011 at 20:43 Comment(0)
D
3

I see the picture, and all your classes are in the same package. So you don't have to import, you can create a new instance without the import sentence.

Dina answered 23/10, 2011 at 20:39 Comment(0)
D
1

First off, avoid using the default package.

Second of all, you don't need to import the class; it's in the same package.

Dorena answered 23/10, 2011 at 20:38 Comment(0)
T
1

If your classes are in the same package, you won't need to import. To call a method from class B in class A, you should use classB.methodName(arg)

Tailband answered 18/9, 2013 at 23:30 Comment(0)
V
0

According Oracle and Sun doc, a class can use all classes from its own package and all public classes from other packages. You can access the public classes in another package in two ways.

  • The first is simply to add the full package name in front of every class name. For example:

    java.util.Date today = new java.util.Date();

  • The simpler, and more common, approach is to use the import statement. The point of the import statement is to give you a shorthand to refer to the classes in the package. Once you use import, you no longer have to give the classes their full names. You can import a specific class or the whole package. You place import statements at the top of your source files (but below any package statements). For example, you can import all classes in the java.util package with the statement Then you can use without a package prefix.

    import java.util.*;

    // Use class in your code with this manner

    Date today = new Date();

As you mentioned in your question that your classes are under the same package, you should not have any problem, it is better just to use class name.

Viperine answered 26/8, 2017 at 8:35 Comment(0)
U
0

If you have the java files under the same package, you will not need to import the classes within those java files. Simply instantiate an object and you will have access. For example, if you need to access class named My_Class from another file within the same package:

My_Class object = new My_Class();

Now, if you put a . dot after typing object, you will gain access to all the methods and attributes from the class.

Now, if you don't have the files under the same package, you can import the Class. Just type:

import Package_Name.Class_name

Where Package_Name represents the name of the package in which the class exists and Class_name being the name of the class you want to import.

Unending answered 16/10, 2022 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.