Grokking Java Interview Questions
Vote

0% completed

​

Nested, Inner, and Anonymous Classes

A class Line is declared inside a class Cart. Small changes to its declaration have large effects on the code that uses it:

  • With static class Line, the expression new Line() compiles in a static method of Cart.
  • With class Line, the same expression fails there: non-static variable this cannot be referenced from a static context.
  • With class Line moved into a method body, other methods of Cart cannot name the class.

The message in the second case mentions a variable this that the expression does not contain

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content