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

3rd Nov 2016, 6:40 PM
Jenny Lance
Jenny Lance - avatar
6 Respostas
+ 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); } }
4th Nov 2016, 12:38 AM
Lucas Lacerda
Lucas Lacerda - avatar
+ 1
x is non static variable. so you can't refer it in a static method. you have to declare it as static.
5th Nov 2016, 9:27 AM
arunkumar M (arun)
arunkumar M (arun) - avatar
+ 1
error
26th Jan 2017, 11:55 AM
AKASH JHA
AKASH JHA - avatar
0
Nothing, an error as you can refer to x from an static content
3rd Nov 2016, 7:11 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
0
replace final by static then Ans is 50 after creating the object, constructor is invoked and object is initialised.
10th Jan 2017, 4:26 PM
true one
true one - avatar
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.
10th Feb 2017, 5:01 AM
Sinan Ceylan
Sinan Ceylan - avatar