2

I have just start learning programming. I get "error: cannot find symbol" if try compile main.java.

main.java
 public class main {
    public static void main(String[] args) {
        Person dima = new Person();
        System.out.println(dima.height);
    }
}
Person.java
 public class Person {
    int height = 189;
}
E:\study\java> javac Person.java
E:\study\java> javac main.java
main.java:3: error: cannot find symbol
                Person dima = new Person();
                ^
  symbol:   class Person
  location: class main
main.java:3: error: cannot find symbol
                Person dima = new Person();
                                  ^
  symbol:   class Person
  location: class main
2 errors
  • Did you import your class `Person` in your `main.java` ? Also see : https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean – Philippe B. Jan 13 '20 at 09:27
  • Exact code you have shared works fine in my machine with output being 189. Could it be that there is some typo or some other change in the code you are trying to run in your machine? – Smile Jan 13 '20 at 09:39

2 Answers2

3

Give a try like this : it is work for me

   class Person{
        int height = 100;
    }

    public class main{
         public static void main(String []args){
            Person dima = new Person();
            System.out.println(dima.height);
         }
    }
  • At least adhere to the Java Naming Conventions when providing an answer. Class names should be written in PascalCase. – MC Emperor Jan 13 '20 at 13:28
0

Thanks for help. I found out how to fix it. Command "set CLASSPATH=" helped me.

  • I suggest you use an IDE like Netbeans, IntelliJ or Eclipse for Java projects. It'll make your life easier, because dependencies like these are automatically resolved for you. – MC Emperor Jan 13 '20 at 13:31
  • Thank you. But i heard for begginers bettere don't use them –  Jan 13 '20 at 13:39
  • That's probably because of their features like autocompletion and code generation. And I partially agree. But once you get a rough idea of what's going on under the hood, then an IDE may be a good idea. – MC Emperor Jan 13 '20 at 13:48