How to import my own class?
Asked Answered
L

5

11

I have this java class :

package myClass;
public class myClass 
{
    private int herAge ;
    public void setHerAge (int herAge)
    {
        this.herAge = herAge ; 
    }
}

and I want to import this class after compile it in another source file called Test.java exists in the same directory , and here what Test.java contains :

import myClass ;
public class Test
{
    public static void main (String []args)
    {
    myClass Rudaina = new myClass();
    Rudaina.setHerAge(30);
    }
}

when I compile Test.java I see this in my console :

Test.java:1: error '.' expected
import myClass ;
              ^
Test.java:1: error '.' expected
import myClass ;
                ^
Limbourg answered 21/2, 2015 at 20:38 Comment(0)
S
10

Your class myClass is also in package called myClass, which can be a cause of confusion.

Try:

import myClass.myClass;

This is called the fully qualified name of the class.

A package is meant to group classes which are conceptually related. Having a package named after a single class is not very useful.

You could name your package after the name of your project. For instance, you could name it

package assignment1;

And then import your class like this:

import assignment1.myClass;
Shalondashalt answered 21/2, 2015 at 20:42 Comment(2)
thank you very much my friend but I have one more question if I want to import myClass without packaging it what I must do ?Limbourg
You don't need to have a package defined for your class. Just don't use one :)Shalondashalt
J
6

While what everyone wrote is true, since the two files are in the same directory, no import should be necessary.

FYI, it is customary to capitalize the names of classes.

Jabez answered 21/2, 2015 at 20:55 Comment(0)
P
2

It's better to put the same package name or a different one as mention below on top of the Test.java file;

package myClass; //or some other name also viable

Then when you compile you can do like this;

javac -d . myClass.java
javac -d . Test.java

The -d specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows), etc. If you want to keep the package within the same directory, you can use the .(dot).

After that use the import statement inside Test.java like this;

import myClass.*;

or

import myClass.myclass;

After that when you run the Test class do like this;

java myClass.Test  //myClass in here is package name if you use some different package name use that
Pilot answered 12/5, 2021 at 13:6 Comment(0)
M
0

You should use the whole class name - including the name of the package.

Memorize answered 21/2, 2015 at 20:43 Comment(2)
and how I can do this ?Limbourg
Use the whole structure of packages - from the first package. If your class is in 3 packages - write "import package1.package2.package3.ClassName;". In your case it looks like "import myClass.myClass;".Memorize
N
0

You are missing the package name. Try import myClass.myClass;

Necrology answered 21/2, 2015 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.