How to create an object for an outer class in a nested class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to create an object for an outer class in a nested class?

please look at the code below. I have also added a comment to highlight the issue there https://code.sololearn.com/crkK6ad6O5Uu

7th Jul 2019, 2:37 AM
salar vahidi
salar vahidi - avatar
2 Answers
+ 2
you need to declare the object in the nested class as static if you want to reference it from static context. when calling nested class static method in outer class constructor it will throw ExceptionInInitializer because the nested class haven't initialized. it will be initialized after object in outer class created. here you can have a look in the code below https://code.sololearn.com/cU9J20rQ4q45/?ref=app
7th Jul 2019, 4:45 AM
Odiesta Shandikarona
Odiesta Shandikarona - avatar
+ 1
thank you so much. it was really helpful. I also learnt about that exception thanks man :)))
7th Jul 2019, 4:35 PM
salar vahidi
salar vahidi - avatar