• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Inner Class Doubts

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K&B Book


What is the meaning of (new Bar() {})? why the open and close curly braces {}? whether it has any meaning?

(new Bar() {}).go();

And don't find any differences in the o/p, when i have used like:

(new Bar()).go();

or

Bar b = new Bar();
b.go();

Just want to know why {} is added? I thought of invalid syntax..
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Micheal John writes....

What is the meaning of (new Bar() {})? why the open and close curly braces {}? whether it has any meaning?


&

And don't find any differences in the o/p.


There is a concept of anonymous inner class in Java which is in picture here.
new Bar(){} is a instance to anonymous inner class which is a subclass of Bar with no extra methods added or overrided.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are actually subclassing the Bar class in when you write the code like Bar{} which can be well understood by executing the following program.

following the sample program with small addition to your code.


public class Foo{
Foo(){System.out.println("foo");}

class Bar{Bar(){System.out.println("bar");}

public void go(){System.out.println("hi");}
}

public static void main(String args[]){
Foo f = new Foo();
f.makeBar();
}
void makeBar(){
(new Bar(){public void go(){System.out.println("hello world");}}).go();
}
}
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hemadri rajy writes...

(new Bar(){public void go(){System.out.println("hello world");}}).go();


THis is perfectly true.
One small add..+
It is valid to add extra method in the anonymous body like

but calling like b.go2() will give a compilation error.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we can use the extra method defined in the anonymous class in follwing possible way...

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic