about Downcasting in java | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

about Downcasting in java

Why this code doesn't work? https://code.sololearn.com/c94TjAaR7c22/?ref=app

16th Oct 2017, 11:59 PM
Hyung Min Lee
Hyung Min Lee - avatar
1 Réponse
+ 2
paragraph from stackoverflow: A non-static nested class in Java contains an implicit reference to an instance of the parent class. Thus to instantiate a Node, you would need to also have an instance of Stack. In a static context (the main method), there is no instance of Stack to refer to. Thus the compiler indicates it can not construct a Node. If you make Node a static class (or regular outer class), then it will not need a reference to Stack and can be instantiated directly in the static main method. See the Java Language Specification, Chapter 8 for details, such as Example 8.1.3-2. [https://stackoverflow.com/questions/13373779/non-static-class-cannot-be-referenced-from-a-static-context] solution: declare the nested classes outside your class or make them static
17th Oct 2017, 12:14 AM
Chrizzhigh - avatar