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"); } }

10th Feb 2017, 1:30 AM
Chandan Kumar Y S
Chandan Kumar Y S - avatar
3 Antworten
+ 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"); } }
10th Feb 2017, 1:52 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
I think you dont need use A.test()...justo test() is fine
10th Feb 2017, 1:42 AM
Javier Hurtado
Javier Hurtado - avatar
0
yeah got it thanks guys @ChaoticDawg and @JavierHurtado....
10th Feb 2017, 2:17 AM
Chandan Kumar Y S
Chandan Kumar Y S - avatar