Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class error in Exam Cram?

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhhhhhh!!! Now I see! Yes, the inner class is static. That explains it! Thanks!!!
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane is right. new NormalClass.InnerClass() should compile fine when InnerClass is declared static.
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks! I have it now! The () was throwing me off.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic