Import a custom class in Java

515,661

Solution 1

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();

Solution 2

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

Solution 3

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();

Solution 4

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.

Solution 5

First off, avoid using the default package.

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

Share:
515,661
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

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

  • Brian Roach
    Brian Roach over 12 years
    It wouldn't matter if it wasn't the default package - you don't have to import classes that are in the same package.
  • Mob
    Mob over 12 years
    @BrianRoach I know. All you need is just an instance. I'm just clearing it up for him.