• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Master exam question on inner class

 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question from master exam


Which inserted at line 7 creates an instance of Bar?

It shows that Only
Foo.Bar b=f.new Bar();
is correct while according to me
Bar b=f.new Bar()
is also correct and it compiles also.Is the answer wrong?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bar b=f.new Bar()
is also correct and it compiles also

How did you compile your program i.e. from an IDE or command line. Also is there any other class named Bar i.e. any Bar.class fine in that folder already??
 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled it on the command line and there was no class named Bar or Foo .Actually i did not use these two names .
I am pasting the code which i compiled. I have checked it once again.


 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see, your code and the code in the question are different. In your code the main method is in the class t1 which has the inner class. That's why you don't need to write the full name t1.t2. If you move the main method to a different class, then you'll need to use the full name...
 
Tanya Shetty
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I referred to this post and then had a look at SCJP 6 book by Kathy sierra and bates, Pg 668 it says,

From outside the outer class instance code (including the static method from within the outer class), the inner class must now be referred to as

Myouter.Myinner

However,it seems that referring the inner class from the static method of the outer class, one can refer the inner class type reference variable as simply .. Myinner
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic