0
What is the error in this code? Regarding non-static in JAVA.
public class A { public static void main(String[] args) { A.test(); } public void test(){ System.out.println("form test"); } }
3 Answers
+ 2
You need to add the keyword static to the method test(). You don't need to reference the A class in your method call to test() here:
public class A{
public static void main(String[] args) {
test();
}
public static void test(){
System.out.println("form test");
}
}
Otherwise you'll need to create an instance of the class and call test from that instance:
public class A{
public static void main(String[] args) {
A a = new A(); // create an instance of the A class
a.test(); // call the test() method from that instance
}
public void test(){ // now static isn't needed
System.out.println("form test");
}
}
+ 1
I think you dont need use A.test()...justo test() is fine
0
yeah got it thanks guys @ChaoticDawg and @JavierHurtado....