aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Master exam question on inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Master exam question on inner class" Watch "Master exam question on inner class" New topic
Author

Master exam question on inner class

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 103
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
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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: 103
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Master exam question on inner class
 
Similar Threads
Confusing Assertions Question
K&B Master Exam Question 68
Assertion Question
Inner Classes question
Doubt in K&B SCJP 5: topic Ch 10 Q 10