Herbert Schildt in The Complete Reference Java 2 says,"Because it is static, it must access the members of its enclosing class through an object. That is, it cannot refer to members of its enclosing class directly. Because of this restriction, static nested classes are seldom used."

Can we use static nested classes practically? If yes, please demonstrate it with help of an example program.

jwenting commented: lazy flooder -3
Ezzaral commented: Really lame just reposting others questions. -1
Sturm commented: Idiot +0

Dani AI

Generated

To : yes — static nested classes are practical and common. Use them when a small type logically belongs to an outer class but does not need direct access to the outer instance. Because a static nested class has no implicit reference to its enclosing instance, it cannot access non-static members directly; that restriction is often a benefit (smaller footprint, no accidental retention of the outer instance, clearer encapsulation).

Example (Builder pattern — a real, practical use):

public class Person {
    private final String name;
    private final int age;

    private Person(Builder b) {
        this.name = b.name;
        this.age = b.age;
    }

    public static class Builder {
        private String name;
        private int age;

        public Builder name(String n) { this.name = n; return this; }
        public Builder age(int a) { this.age = a; return this; }
        public Person build() { return new Person(this); }
    }

    public String toString() { return "Person[name=" + name + ", age=" + age + "]"; }

    public static void main(String[] args) {
        Person p = new Person.Builder().name("Alice").age(30).build();
        System.out.println(p);
    }
}

When to pick static nested vs inner:

  • Use static nested when the nested type does not need access to instance fields of the outer class.
  • Make it private if it is an implementation detail (e.g., private static class Node in a list).
  • Use a non-static inner class only when you need an implicit outer reference.

This thread duplicated an earlier discussion (as noted) and was closed by ; consult the original for more examples and follow-ups.

Recommended Answers

All 6 Replies

get lost. You (or was that someone else) was told this already just a few days ago.

Jishnu was the one who asked a few days ago. This guy is definitely just reposting questions at this point and hopefully on the fast track to a ban.

How dare he?! This does not make sense at all! He is reposting the question I had asked some time back in a separate thread. Does he think the people at DaniWeb are fools ?? He is filling the C and C++ forums also with silly questions. Hopefully he will be banned.

I wonder why this thread is not closed...

closed

commented: Thank you for banning that guy. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.