Possible Duplicate:
Static nested class in Java, why?
I know about the static fields in java. But don’t know why we create a static class. Any idea?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Normally, an inner class may reference its owning class’s fields, as you can see in the (pseudo) sample above. So an instance of Inner may access fields / methods / etc in Outer.
But this requires every Inner instance to have an Outer instance associated with it. So you cannot do
but instead must do
In some cases this is not desirable – say you need to ensure that your Outer instances may get garbage collected, or that you need to make Inner instances independent of Outer. In that case you may change it to
in which case now you may no longer reference “a” as before, but in exchange you don’t need to have an instance of Outer at all to have instances of Inner.