• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

could anybody kindly explain what the output would be and how you arrived at it

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Foo {
Foo() {System.out.print("foo");}
class Bar{
Bar() {System.out.print("bar");}
public void go() {System.out.print("hi");}
}
public static void main (String [] args) {
Foo f = new Foo();
f.makeBar();
}
void makeBar() {
(new Bar() {}).go();
}
}
what is the result?
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi

Could anybody kindly explain what the output would be and how you arrived at it
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is foobarhi.This is because you could see that from the main method a new foo object is created.So its constructor runs and foo is printed.Afterwards it calls makebar() function it makes a call to the constructor of the barclass

the important point to note is

1. (new bar() {}).go()
2. new bar().go()

are absolutely same here since the bar inner class has a default constructor the statement two it have become invalid if there is no empty constructor and provided an argumented constructor is present.
so the bar constructor runs and it prints "bar".next in the go method an
System.out.println("hi");
is called which gives "hi"

so the answer is foobarhi
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"sreeprasad sp",
Please check your private messages.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sree visu,
Please, in the future, don't answer homework questions here.
Multiple choice questions are almost always either homework or a test question and we prefer that the original poster work these things out for themselves before coming here for help.

sreeprasad sp,
Do you have a Java compiler on your machine?
If so, have you compiled this and run it?
What output did you see?
Give us your explanation for how the program works, and we'll be happy to tell you why you are right or wrong but in the future, please do not post these types of questions here.

See:
http://faq.javaranch.com/java/DoYourOwnHomework
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic