aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class error in Exam Cram? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class error in Exam Cram?" Watch "Inner class error in Exam Cram?" New topic
Author

Inner class error in Exam Cram?

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

On pg. 94 of the Exam Cram book, the correct answer is supposed to be a. NormalClass.NestedClass myNC = new NormalClass.NestedClass(); This is supposed to be the correct way to declare and initialize a reference to an inner class (without also making a reference to the enclosing class), however I cannot make this work on my machine. The only way I can make it work is to insert new in front of NestedClass(); like this NormalClass.NestedClass myNC = new NormalClass. new NestedClass(); I believe this is an error in the book. Can someone confirm this? Thanks!!!
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Barry,
I (and may be a few others too) don't/may not have the Exam cram book so it'd be a good idea to post your code here. Is your inner class declared static? If your inner class is declared static, then new NormalClass.NestedClass() should work fine.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Ahhhhhhhh!!! Now I see! Yes, the inner class is static. That explains it! Thanks!!!
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Okay, now I am confused again. Here is the code:
class NormalClass
{
static class InnerClass
{}
public static void main(String[]args)
{
NormalClass.InnerClass ic = new NormalClass(). new InnerClass();

}
}
Now this works fine on my machine, but if I take out the second new it will not compile. I do not understand!

Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Barry,
In the code you have posted, I found that the argument for the main method is specified as String[]args, with no gap between String[] and args. I hope that is not causing the compile error. Otherwise, your instance declaration and initialization should work fine. Can you exactly tell us what error you get?
Jane Zheng
Greenhorn

Joined: Oct 17, 2000
Posts: 11
I think you have to remove the () which follows the outer class name, it would be compiled fine if it is:
NormalClass.InnerClass ic = new NormalClass.InnerClass();
Jane
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Jane is right. new NormalClass.InnerClass() should compile fine when InnerClass is declared static.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Thanks folks! I have it now! The () was throwing me off.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
That one catches a lot of people. Especially since I have two example questions that look almost alike - the distinction being that in one case the inner class is static.
Bill

Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class error in Exam Cram?
 
Similar Threads
Inner class references
declare and initiate a reference to inner class
a static inner class question
Declaring and initializing a reference to a Nested Class
Is This Incorrect??