Java Programming - Inner Classes - Discussion

Discussion :: Inner Classes - Finding the output (Q.No.1)

1. 

What will be the output of the program?

public class Foo 
{
    Foo() 
    {
        System.out.print("foo");
    }
    
class Bar
{
    Bar() 
    {
        System.out.print("bar");
    }
    public void go() 
    {
        System.out.print("hi");
    }
} /* class Bar ends */

    public static void main (String [] args) 
    {
        Foo f = new Foo();
        f.makeBar();
    }
    void makeBar() 
    {
        (new Bar() {}).go();
    }
}/* class Foo ends */

[A]. Compilation fails.
[B]. An error occurs at runtime.
[C]. It prints "foobarhi"
[D]. It prints "barhi"

Answer: Option C

Explanation:

Option C is correct because first the Foo instance is created, which means the Foo constructor runs and prints "foo". Next, the makeBar() method is invoked which creates a Bar, which means the Bar constructor runs and prints "bar", and finally the go() method is invoked on the new Bar instance, which means the go() method prints "hi".


Vidur Punj said: (Feb 25, 2012)  
(new Bar() {}).go();
It should produce compile time error ;

Chandni said: (Apr 4, 2013)  
Why use{} after calling of Bar() in makeBar() method?

Keshav Kushwaha said: (Apr 16, 2013)  
Compilation fail : "The public type Foo must be defined in its own file".

Jalpesh said: (Jul 31, 2013)  
@Keshav kushwaha : you should rename your java class file name as Foo.

Sohi said: (Sep 11, 2013)  
@Chandni.

Its just to confuse the developer but syntax is creating an anonymous subclass of Type Bar and Using Bar class's(which is super class of Anonymous class)go() method.

Shanika said: (Feb 15, 2014)  
@Sohi you are right. But the Anonymous class will not inherit the constructor right? Therefore no way of printing "bar" in the result.

Rajeshwari said: (Sep 26, 2014)  
Am not getting how to call inner class constructor. To create anonymous inner class object we use different syntax i.e we need outer class reference variable.

Please help!

Yugandhar said: (Feb 23, 2015)  
Hi all,

Can someone explain about:

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

I really confused.

1) How it is anonymous class?
2) How we are able to create obj with new and anonymous class?
3) How bar constructor is getting called?

Sonali said: (Aug 12, 2015)  
What is the concept of go in this code?

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

Emre Bayram said: (Nov 30, 2015)  
In Java, when a subclass is created, it automatically calls its parent's constructor (if the class itself does not define constructors).

You have a in-the-air class definition here which extends Bar. And you create an instance of this class which has no constructor so first Bar's constructor is called then go () is called (new Bar() {}).go();

Sumanth said: (Dec 14, 2016)  
foo f = new foo();
f.makeBar();
=========

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

Look at these statements closely.

Step 1:
foo f = new foo(); its Foo class constructor will be initiated first .. so "foo" will be printed (since it is system.out.print ) foo will be printed output. The output continues to print the same line

Step 2: (new Bar() {}).go(); statement.. printS barhi.., So all together output will be foobarhi.

Please correct me if I am wrong.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.