class A{}
class B extends A {}
class C extends B {}
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 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
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
. 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
Jeanne Boyarsky wrote:You could try it and see what the output is. Asking why on the internet is more helpful than asking strangers to run your test examples for you.
I'll pretend you did that and explain why that is not the output.
The constructor for both enums runs. Which prints cp twice and increments count. Then the println with the count in it runs.
Henry Wong wrote:
henry joe wrote:
So, what does the block mean in this code, why is the syntax written this way
Well, it's an instance initializer -- which is executed (in textual order) along with initializations of instance variables.
Please, I am just so curious about this instance initializer. I would, finally, like to know why it was used in the code fragment? I never come across such syntax until now. So, what is the use?
Thank
Jeanne Boyarsky wrote:
1) No, it is not a static block. You can tell because the keyword static does not appear immediately before it.
.
Jeanne Boyarsky wrote:To be more explicit about the source: K&B, practice exams, self assessment test #2.
1) No. When you learn about inner classes, you will see an enum can be an inner class instead of outside a class. For example, this code is fine:
2) An enum creates all the values at the same time, not when you first reference them. This means the two constructors are called one after another. And both are called before the println runs to output the counts.
Jeanne Boyarsky wrote:To be more explicit about the source: K&B, practice exams, self assessment test #2.
The code calls binary search with va[0] as a comparator. Also known as a descending sort.
So your question is, given the array:
a[0] = 200, va[1] = 60 and va[2] = 40
where does 80 go in the list when sorting descendingly.
80 goes between 200 and 60. Which means if we inserted it at index 1 (and moved everything over one), we'd have the descendingly sorted list: 200, 80, 60, 40.
Java then takes that index 1, makes it negative (-1) and subtracts 1 (-2).
Paweł Baczyński wrote:The result is:
[3, 7, 5, 9, 1]
Note that in doStuff() you are adding "9" do list named x and then the reference to that list is stored in variable y.
This results in x and y pointing to the same instance of the list.
So, whatever you do to y is reflected in x.
You are wrong about new ArrayList being created. The only thing that happens is passing reference to an ArrayList object created at line 4.