0

I'm new to Java, so if the code looks overly silly, please bear with me. :-)

What I'm trying to achieve is simple: Define two classes ClassA and ClassB, which are both part of the same package find_class (this is also the name of the directory where the classes are.

So my files are as follows.

ClassA.java

package find_class;

public class ClassA {
    public void func() {

    }
}

ClassB.java

package find_class;

import find_class.ClassA;

public class ClassB {
    public static void main(String[] args) {
        ClassA a = new ClassA();
    }
}

When I compile ClassB, I run into a number of errors:

find_class$ javac ClassB.java 
ClassB.java:3: error: cannot find symbol
import find_class.ClassA;
                 ^
  symbol:   class ClassA
  location: package find_class
ClassB.java:7: error: cannot find symbol
        ClassA a = new ClassA();
        ^
  symbol:   class ClassA
  location: class ClassB
ClassB.java:7: error: cannot find symbol
        ClassA a = new ClassA();
                       ^
  symbol:   class ClassA
  location: class ClassB
3 errors

I'm not using any IDE and the directory structure is as follows:

find_class
  - ClassA.java
  - ClassB.java

What am I doing wrong?

ankush981
  • 5,159
  • 8
  • 51
  • 96
  • Remove import find_class.ClassA; – bane19 Nov 15 '16 at 11:48
  • are you using an IDE? A good IDE would point out these issues to you and help you resolve them. – L. Young Nov 15 '16 at 11:57
  • Just verify the package structure in your build path of the project.your class is in find_class directory but it will be as /project/src in your build path – Zia Nov 15 '16 at 12:00
  • @L.Young No, I'm not using an IDE. I want to learn these finer details first, so I'm sticking to a text editor. – ankush981 Nov 15 '16 at 12:06
  • @Zia Hi, there is no `src` directory here. I'm using a text editor and these two classes are in a directory called `find_class`. – ankush981 Nov 15 '16 at 12:07
  • @dotslash don't underestimate an IDE. You may think you'll learn better without it, but an IDE teaches you as you try things if you pay attention to the hints it gives and understand why it gives them. – L. Young Nov 15 '16 at 12:17
  • @L.Young Hey, I don't underestimate them. In fact, I want to start using IDEA + Gradle soon. But I've read a lot of advice that one should avoid IDEs at the start. – ankush981 Nov 15 '16 at 12:20
  • 1
    @Tom Thanks! It's indeed a duplicate and my question got answer while reading the most-upvoted answer. :-) – ankush981 Nov 15 '16 at 12:24
  • @L.Young Just wanted to provide a closure to the IDE thing. I just spent half a day figuring out dependencies in a `Unirest` project and finally decided to give Eclipse a try. Pretty painless, I must say! :-) – ankush981 Nov 15 '16 at 17:47
  • @dotslash to each his/her own :) Everyone has preferences and I'm not one to judge, I just personally found it very helpful it the learning process :) – L. Young Nov 16 '16 at 13:17

3 Answers3

3

The package name and the class path should sync. In other words, the package name is a continuation of the class path.

For example, if the folder structure is x/y/z, the class path is x/y, and the package name is z, then you shouldn't import y.z

Applying the same logic to your case, your class path is already set to "find_class". So, you don't need the import statement.

attaboy182
  • 2,039
  • 3
  • 22
  • 28
  • So, by default the class path is set to the current directory? Actually, removing both the import statement and the package statements makes it work. – ankush981 Nov 15 '16 at 12:10
  • class path is something that you can set explicitly: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html – attaboy182 Nov 15 '16 at 12:28
0

Your class says it is in Package "find_class", the class knows about all the other classes in its package, so you do not need to import ClassA, as it can see it.

Remove the line

import find_class.ClassA;
Phased
  • 105
  • 1
  • 7
  • 1
    And how should removing a redundant import fix his issue? The issue is somewhere else. – Tom Nov 15 '16 at 12:05
  • @Tom Exactly my point! By the way, removing the `package` statement seems to work, but it looks strange. Is that how it's supposed to be? – ankush981 Nov 15 '16 at 12:16
  • @dotslash You've provided a classpath for your compilation, right? – Tom Nov 15 '16 at 12:23
  • 1
    @Tom I just responded to your comment on the question. It was a problem of classpath only. :-) – ankush981 Nov 15 '16 at 12:25
0

You do not need to import other class.For example if you want to use method from other class you should use objects like you did.

ClassA a = new ClassA();

By the way u cant import classes if there are in same package.

Ugur Tufekci
  • 350
  • 2
  • 13
  • 2
    *"By the way u cant import classes if there are in same package."* *can't* is wrong. It is not prohibited to do so. – Tom Nov 15 '16 at 12:04