What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?
Share
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.
From the Java Tutorial:
Static nested classes are accessed using the enclosing class name:
For example, to create an object for the static nested class, use this syntax:
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
see: Java Tutorial – Nested Classes
For completeness note that there is also such a thing as an inner class without an enclosing instance:
Here,
new A() { ... }is an inner class defined in a static context and does not have an enclosing instance.