Java Syntax I've never seen

While reading an inner classes article I discovered some Java syntax totally new to me..
Say you write a public inner class like so.


public class Foo {
  public class Bar {}
}

If I wanted to create a new instance of Bar I would have to do it like so..


Foo foo = new Foo( );
Foo.Bar bar = foo.new Bar();

The foo.new is something that I have never seen before but it is refreshing to see something new like this in a language that you know so well. I suppose it fits with my understanding of the language since all constructors are compiled into static <init>() methods, and Bar’s constructor is in Foo’s namespace. After all new Bar() is just syntactic sugar for the static Bar.<init>() method in compiled code.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Reddit
  • StumbleUpon

Tags:

Leave a Reply