+ 2
What is Output?
class A { final int x; { x= 23+27; } public static void main(String arts[]) { A ob = new A(); System.out.println(x); } }
6 Réponses
+ 1
To solve the problem just now exchange "final" with "static". Ex:
class A
{
static int x;
{
x= 23+27;
}
public static void main(String arts[])
{
A ob = new A();
System.out.println(x);
}
}
+ 1
x is non static variable. so you can't refer it in a static method. you have to declare it as static.
+ 1
error
0
Nothing, an error as you can refer to x from an static content
0
replace final by static then
Ans is 50
after creating the object, constructor is invoked and object is initialised.
0
There are two error in this code:
You referenced non-static constant (x) in static method.
Final is a keyword that describes your entity is a constant, that means you can't change its value. So, an assignment to x is irrelevant. To solve the problem change your entity to a variable by removing final keyword and make it static to call it from a static method by adding static keyword. Ex. static int x;
That's all.