• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A mistake in K&B book?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I sent that message at Kathy's e-mail address mentioned in the book, but received no response. I also searched the archive of that forum if someone had mentioned that before, but found nothing, so I decided to put it here.
In the self test area for chapter 8 - Inner Classes I found the following question:
"1. Given the following,
public class MyOuter{
public static class MyInner{
public static void foo(){}
}
}
which statement if placed in a class other then MyOuter or MyInner, instantiates an instance of the nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. MyOuter m = new MyOuter();
MyOuter.MyInner mi = m.new MyOuter.MyInner();
D. MyInner mi = new MyOuter.MyInner();"
In the answer section it says that the correct answer is A, and this is correct, but in my oppinion B, and D are also correct. To prove it I wrote the following test:
// firstly the class from the question #1 chpt8
package scjp.chpt08;
public class MyOuter{
public static class MyInner{
public static void foo(){}
}
}
// in another package
package scjp.testit;
import scjp.chpt08.MyOuter;
import scjp.chpt08.MyOuter.*; /* you can do this, and it makes sense if there are inner classes in MyOuter */
public class OuterInnerTest {
public static void main(String[] args) {
MyOuter.MyInner in1 = new MyOuter.MyInner();
MyOuter.MyInner in2 = new MyInner();
MyInner in3 = new MyOuter.MyInner();
// you can even add the line below and it will work too.
MyInner in4 = new MyInner();
}
}
So, the requirements for that question are met - the statement is placed outside MyOuter/MyInner (in another package as a matter of fact) and both "B" and "D" work (at least my Eclipse framework with JDK 1.4.2_02 instaled does not protest about it).
And the same about the "two minute-drill" for that chapter - I mean the key point before the last one. In the light of the above example seems not to be true.
Anybody has an idea how would that answer be marked on the real exam???
Regards
Chris
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I imagine Kathy and Bert get a ton of e-mails and have a hard time keeping up with them all (although, from what I know of them, I'm sure they work very hard to do so). You might want to check the errata for that book to see if the error is already noted.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I think in the book all is right.
I tryed this code and answers in IDEA 4 and JBuilder 10 and thy are accept only first answer. Other is not compiled.
 
Krzysiek Hycnar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well... that sounds strange I still have the project in my Eclipse IDE and it compiles without a problem. Apart from that, I use that kind of code all the time in my company projects (the platform is mainframe under OS/390 and z/OS, the compiler is Java 1.3 compatibile) and have NO problems to compile & run it (actually the fact I use it all the time and it works for me, made me start thinking there must be something wrong with that question). A friend of my has just checked the code in JBuilder 4 (a very old one ) and it works for him too.
Chris
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that you are doing that correctly. Note that it says "which statement if placed in a class other then MyOuter or MyInner". Since the name of the static class is MyOuter.MyInner there is no way that B & D can compile correctly from a class other than MyOuter.
 
Krzysiek Hycnar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Thomas:
Have you read my original post?? All the code I'm talkin' about is included there.

"// in another package
package scjp.testit;
import scjp.chpt08.MyOuter;
import scjp.chpt08.MyOuter.*; /* you can do this, and it makes sense if there are inner classes in MyOuter */
public class OuterInnerTest {
public static void main(String[] args) {
MyOuter.MyInner in1 = new MyOuter.MyInner();
MyOuter.MyInner in2 = new MyInner();
MyInner in3 = new MyOuter.MyInner();
// you can even add the line below and it will work too.
MyInner in4 = new MyInner();
}
}"
So you can see that the lines that attempt to instantiate MyInner instances are placed in OuterInnerTest which is "other then MyOuter or MyInner"
To all of you, interested in this topic:
If this would be helpful, I can forward anyone of you the project source (in a zipped archive). If you use Eclipse, you will be able to import the code and check it yourself in your IDE.
Cheers
Chris
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I think that your answer is correct, as well as K&B's. The difference is your import statement:
import scjp.chpt08.MyOuter.*;
This makes the inner class 'known', so that B & D work. The question does not mention an import, though.
Greetings, R�diger.
 
Krzysiek Hycnar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly R�diger!!
But you CAN place the "import scjp.chpt08.MyOuter.*;" statement - the description of the question does not forbide you to do this.
What's more if you choose to instantiate MyInner in another package you're even suppossed to use sth. like "import scjp.chpt08.*;" (what that kind of questions silently assume). Anyway, IMHO placing "import scjp.chpt08.MyOuter.*;" does NOT break the rules of the question, which means the question is not as specific as it should be (because I managed to find a way to make "B" & "D" work without breaking the rules of it).
Finally the "two-minute drill" key points... the one before the last for chapter 8 that says:
"Instantiating a static nested class requires using both the outer and nested class name as follows:
BigOuter.Nested n = new BigOuter.Nested();"
is simply not true, because I showed that you can do without the outer.
Now the big question is, if "surprises" like that can be found on the real exam. Maybe Bert or Kathy could cut my/our doubts just saying... "this is not on the exam"
Cheers
Chris
 
Krzysiek Hycnar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even funnier piece of code
package scjp.chpt08;
import scjp.chpt08.MyOuter.*; //
class MyOuter{
public static class MyInner{
}
}
public class TestIt{
public static void main(String[] args) {
MyInner in = new MyInner();
}
}
Taaaa daaaa - it works too
Chris
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package scjp;

import scjp.MyOuter.MyInner;

public class NestedTest
{
/**
* @param args
*/
public static void main(String[] args)
{
MyInner inner = new MyInner();
}
}

Well and this one too...
I'm beginning to hate this BE A JAVA-COMPILER SCJP and answer stupid questions exam.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roland,

Welcome to JavaRanch. I don't know if you noticed that this thread is about three years old

It's probably a lot less confusing for you to start up a new thread as opposed to "resurrecting" an old thread like this.

Thanks,

Bert
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krzysiek Hycnar ,

When trying to compile the code..with jdk5.0 i found that Answer A is the only correct answer and all the other options gives compiler error
 
Roland Schneider
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bert I wanted to discuss this and new K&B errors in the Errata Thread. But the thread is locked,
is there another one?

@cherish B&D works by using imports. So you don't have to use the full qualified classname.
[ September 25, 2007: Message edited by: Roland Schneider ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roland,

The best thing to do is to just start another thread and give it a meaningful title like "doubt about X in k&B"

Thanks,

Bert
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert

Its an honour to talk to you..Thanks to javaranch
So how to see the errata page..how to know the errors??I want to see the earlier discussions that happened...

Many Thanks
Tashi
 
Tashi Rautela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert

Its an honour to talk to you..Thanks to javaranch
So how to see the errata page..how to know the errors??I want to see the earlier discussions that happened...

Many Thanks
Tashi
reply
    Bookmark Topic Watch Topic
  • New Topic