0
May I know what's the problem in the code
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
+ 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);
}
}