Grokking Java Interview Questions
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 expressionnew Line()compiles in a static method ofCart. - With
class Line, the same expression fails there:non-static variable this cannot be referenced from a static context. - With
class Linemoved into a method body, other methods ofCartcannot 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%