. I tried this as shown in the second code below and it compiled BUT in the book, the answer is option A which is null instead of C which is the best answer given the code in the text book is NOT correct and they gave this explanation :No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).
. Given the code in the book, I couldn't even get it to compile. I tried going by the instruction given by the error message and modified it as shown below :A is correct. The Friends class doesn't override equals() and hasCode(), so the key to the HashMap is a specific instance of Friends, not the value of a given Friends instance's name
henry joe wrote:In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :
Unfortunately, this code doesn't compile, giving the following error :.No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).
Henry Wong wrote:
henry joe wrote:In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :
Unfortunately, this code doesn't compile, giving the following error :.No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).
The reason it doesn't compile is because the Friends class is an inner class, and you can't instantiate an instance of an inner class without an instance of it's outer class.
Now, we could try to work with you to figure out how to use the inner class, but something tells me that you entered it wrong. In the book, have you learned inner classes yet? Meaning is the Friends class supposed to be inner class? Or should it have been just another class ?
[EDIT. Never mind. It looks like your example partially uses the inner class as an inner class, and partially doesn't. The part that doesn't is the incorrect part]
Henry
henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
Henry Wong wrote:
henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
Perhaps you can tell us what you think it should be, if it is not null.
Henry
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
henry joe wrote:
Henry Wong wrote:
henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
Perhaps you can tell us what you think it should be, if it is not null.
Henry
Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
henry joe wrote:
Henry Wong wrote:
henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
Perhaps you can tell us what you think it should be, if it is not null.
Henry
Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?
Well, it is very simple actually. You put something in the Map and you are trying to retrieve it back. The way a map works is that first you associate a key with a value and to retrieve the value later on, you need to give that same key. Here, "same" means that the object that you gave as a key earlier should be "equal" to the object that you are giving now to retrieve the value. i.e. key1.equals(key2) should return true.
Do you think that is happening in your code for it to not print null?
henry joe wrote:
Paul Anilprem wrote:
henry joe wrote:
Henry Wong wrote:
henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
Perhaps you can tell us what you think it should be, if it is not null.
Henry
Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?
Well, it is very simple actually. You put something in the Map and you are trying to retrieve it back. The way a map works is that first you associate a key with a value and to retrieve the value later on, you need to give that same key. Here, "same" means that the object that you gave as a key earlier should be "equal" to the object that you are giving now to retrieve the value. i.e. key1.equals(key2) should return true.
Do you think that is happening in your code for it to not print null?
Thanks alot for the clarity. According to the text book, that code was wrong and I had thought the answer would be C which is : it won't compile. The reason is because you have to access the Friends methods through the Birthdays Object since Friends is an inner class but in the code, that was NOT the case. So, why did they give a wrong code and instead of choosing C as the answer, they chose D as the answer?
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Don't get me started about those stupid light bulbs. |