How do you use a variable declared in an anonymous class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you use a variable declared in an anonymous class?

Obj object = new Obj(){ public int ID = 1; }; How can you reach the ID value which is declared in the instantiation of the object instead of its own class. If that isn't possible then why am I able to write this code and get no compilation errors?

5th May 2018, 2:10 PM
Robert Sokolov
Robert Sokolov - avatar
2 Answers
0
object.ID I suppose
5th May 2018, 2:37 PM
Bebida Roja
Bebida Roja - avatar
0
No, that's not possible. But I've found a workaround for this, by extending the Obj class in the class where the object is instantiated and declaring ID inside the child class.
5th May 2018, 2:48 PM
Robert Sokolov
Robert Sokolov - avatar