File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested classes

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can nested classes have constructors??
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All nested classes except anonymous inner classes can have constructors...
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...

no special rules apply?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit is right. If you want to be overly technical, you could say that all classes can (and indeed must) have constructors. When you don't explicitly declare a constructor, the class obtains the default constructor provided by the compiler. In the case of an anonymous inner class, it is clear why you can't have an explicitly defined constructor, because the constructor must have the same name as the class, but...what's the name of an anonymous class? However, the anonymous inner class must get a default constructor that simply calls the super() constructor of the class extended by the anonymous class (if the anonymous class extends a class.) If the anonymous class instead implements an interface, and given that interfaces do not have constructors, the anonymous class will get a default constructor that doesn't do anything.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem with instantiating an innner class from a static context...Could you tell me where I'm going wrong??

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Inner4 is method local class, so just instantiate is as line1.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner4 looks like a method-local inner class. You need to instantiate it the same way that you are instantiating Inner3. The only difference is that since Inner4 is inside a static method, Inner4 won't be able to access any instance members from the enclosing class, just static members.

Edit: What Punit said. He must have submitted his reply while I was writing mine.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah good...

I can see the pattern in syntax for instantiating inner classes now
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic