0

I am trying to compile my code in Java but every time I try it gives me the error saying it cannot find the symbol of the other class. For example:

package helloWorld;
public class FirstClass {

       public static void main(String[] args) {
              SecondClass secondClass = new SecondClass();
              class.HelloWorld();
       }

}

And my other class will be:

package helloWorld;
public class SecondClass {

        public void HelloWorld() {
               System.out.println("Hello World");
        }
}

If I try to compile the code it will point at SecondClass declaration and say, error: cannot find symbol. The code would run fine in an IDE like Eclipse or NetBeans.

  • Are the two classes in the same directory? Are the files named like the class names? Have you used a search engine with that error message? Have you read [mcve] to understand how to ask a question we could actually answer? – GhostCat Sep 13 '20 at 04:36
  • Just edited it, let me know if there are still issues. Sorry about that. – CrispyNuggets Sep 13 '20 at 04:43
  • Well, your if your edited code is correct, then this is a simple typo. You declared a variable "secondClass", but then want to call a method on a variable named "class" – GhostCat Sep 13 '20 at 05:06

2 Answers2

1

class is one of the keyword, but you are tyring to use as a variable name

 SecondClass class = new SecondClass();
 class.HelloWorld();

Please change the variable name to different name than class and retry.

You cannot use keywords like int, for, class, etc as variable name (or identifiers) as they are part of the Java programming language syntax.

I made following changes and the ran successfully

public class FirstClass {
public static void main(String[] args) {
           SecondClass class1 = new SecondClass();
           class1.HelloWorld();
       }
}

Directory of D:\workspace_europa\DatastructureAndAlgorithms\src

09/13/2020  10:06 AM    <DIR>          .
09/13/2020  10:06 AM    <DIR>          ..
09/13/2020  10:11 AM               330 FirstClass.class
09/13/2020  10:06 AM               168 FirstClass.java
09/13/2020  10:11 AM               408 SecondClass.class
09/13/2020  09:57 AM               131 SecondClass.java


D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -cp  . FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -cp  . FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\java.exe -cp  . FirstClass
Hello World

If you are still facing issue then using -verbose flag during compilation and runtime can give you the lead

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -verbose -cp .  FirstClass.java


D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\java.exe -verbose -cp .  FirstClass
Kousik Mandal
  • 686
  • 1
  • 6
  • 15
0

Try to run it from the parent folder. Like if your package is kept in the A folder then run the program like this -

javac A/helloWorld/FirstClass.java
java A/helloWorld/FirstClass

This should work.