First and foremost I want to make myself clear: I am not asking what the cannot find symbol
error means, I am simply asking what is causing this error in this context.
I have recently delved into classes in Java. Below is my first [non main
] class:
class Test {
public void test() {
System.out.println("Hello, world!");
}
}
class Main {
public static void main(String[] args) {
test();
}
}
But I get the following error:
exit status 1
Main.java:8: error: cannot find symbol
test();
^
symbol: method test()
location: class Main
1 error
Can anyone please explain why this happens?
System.out.println("Thanks!");
The method test() is not declared static.
You are calling a non-static method test() in a static method main(). If you do not want to change the class Test you have to change main() as follow
public static void main(String[] args) {
Test t = new Test();
t.test();
}
If you do not want to change main() too much. Then you have to change the test() method as follow: public static void test() {}
and the inside the main() method:
Test.test()