| Author |
Master exam question on inner class
|
ujjawal rohra
Ranch Hand
Joined: Mar 20, 2010
Posts: 101
|
|
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?
|
SCJP 6
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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??
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
ujjawal rohra
Ranch Hand
Joined: Mar 20, 2010
Posts: 101
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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
Joined: Jun 17, 2009
Posts: 40
|
|
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
|
 |
 |
|
|
subject: Master exam question on inner class
|
|
|