Thursday, March 12, 2009

What is the disadvantage of using an inner class? (CoreJava)

Question :What is the disadvantage of using an inner class? (CoreJava)
Answer :Java bytecode has no concept of inner classes, so the compiler translates
inner classes into ordinary classes that are
accessible to any code in the same package. An inner class gets access to
the fields of the enclosing outer class-even if
these fields are declared private and the inner class is translated into a
separate class. To let this separate class
access the fields of the outer class, the compiler silently changes these
fields' scope from private to package. As a
result, when you use inner classes, you not only have the inner class
exposed, but you also have the compiler silently
overruling your decision to make some fields private.

No comments: