What is the difference between inner class and static nested class in Java?
Hello everyone! I started learning Java and got confused about nested classes. I read the Oracle documentation, but it's too dry. There are two types: inner class and static nested class. I wrote a small example, but I don't understand why the code works differently.
Here's my code:
```java
public class OuterClass {
private String outerField = "outer field";
// Inner class
class InnerClass {
void display() {
System.out.println("Inner: " + outerField);
}
}
// Static nested class
static class StaticNestedClass {
void display() {
// Error! Cannot see outerField
// System.out.println("StaticNested: " + outerField);
}
}
}
```
Questions:
No answers yet.
Log in to leave an answer
Blogs