0

May I know what's the problem in the code

https://code.sololearn.com/c2ZlwkoD6932/?ref=app

22nd May 2019, 7:25 AM
Sudheshna
Sudheshna - avatar
2 Respuestas
+ 1
variable 'x' is defined in main function and hence the method 'function' can't find it. Declare'x' as a instance variable (before starting any function) and the program will work correctly
22nd May 2019, 7:36 AM
Shashwat Sahay
Shashwat Sahay - avatar
+ 6
You may want to arrange it like this, remeber that static variables belong to the class and not the object they have a class scope and cant be declared inside of a methods not even the main method this is the same for static methods. public class Program { static int x=0; Program() { x++; } public static void main(String[] args) { Program p1=new Program(); Program p2=new Program(); Program p3=new Program(); System.out.println(x); } }
22nd May 2019, 10:00 AM
D_Stark
D_Stark - avatar