Why my following code isn't working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why my following code isn't working

Problem: I want to print the 11 as output! My Code: class Abhishek { public static void main(String[] args) { int[][][] sample00 = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}} }; } int x = sample00[2][1][0]; System.out.println(x); }

21st Aug 2021, 4:48 AM
Abhishek Kumar
4 Answers
+ 8
All statement lines should be inside the main function. Move the 'int x' and 'System..' lines up inside the bracket, and it will work.
21st Aug 2021, 4:59 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Tibor Santa Actually you can have static initializers outside of any static method. They are useful to initialize static fields without having to call an init() method. Still i would prefer the Singleton approach to that https://code.sololearn.com/c1MX5yj70465/?ref=app
21st Aug 2021, 8:59 AM
Giorgos
+ 2
Thanks Giorgos D I know about static blocks, but I haven't really had any need to use them so far. Anyway I guess the question here is on a more basic level.
21st Aug 2021, 9:19 AM
Tibor Santa
Tibor Santa - avatar
0
Solution: class Abhishek { public static void main(String[] args) { int[][][] sample00 = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}} }; int x = sample00[2][1][0]; System.out.println(x); } //int x = sample00[2][1][0]; //System.out.println(x); }
24th Aug 2021, 12:34 AM
Abhishek Kumar