-2

I'm trying to make different classes and make instances in the main class then run the program just that simple, but i get this error:

shka.java:4: error: cannot find symbol
        ahmed c = new ahmed("Shika");
        ^
  symbol:   class ahmed
  location: class shka
shka.java:4: error: cannot find symbol
        ahmed c = new ahmed("Shika");
                      ^
  symbol:   class ahmed
  location: class shka
2 errors
error: compilation failed

And here is the code shka.java:

public class shka {
    public static void main(String[] args) {
        System.out.println("Starting.. ");
        ahmed c = new ahmed("Shika");
        // c.name = "Shika";
        System.out.println(c.name);
    }
}

ahmed.java:

public class ahmed {
    public String name;

    // Constructor
    // This = self in python
    public ahmed(String name) {
        this.name = name;
    }

    public void msg() {
        String h = "BATTA";
        System.out.println("HELLO, " + h + " This is the other class");
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – JCWasmx86 Jul 13 '20 at 08:44
  • No it didn't @JCWasmx86 –  Jul 13 '20 at 08:53
  • Are both files in the same directory? Try to compile with `javac *.java`, to compile all files – JCWasmx86 Jul 13 '20 at 08:54
  • Yes they are, i'm compiling using 'java filename.java', does it make difference? –  Jul 13 '20 at 08:58
  • Yea, that link kind of assumes a level of knowledge of (basic) Java that it seems that you don't have yet. – Stephen C Jul 13 '20 at 08:58

1 Answers1

2

The problem is that you don't have package statements in the classes.

A class without a package statement is implicitly declared in the default (anonymous) package. But a class in the default package is not implicitly imported by another class in the default package. AND you can't explicitly import from the default package ... because it has no name.

Solution:

  1. Read about Packages. This is the most important step.
  2. Add package statements
  3. If the classes are in different packages (your choice!) add import statements as required.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216