Again and again, we search for other people's mistakes and correct them... That's our lot in life as programmers. Our program again displays something wrong: An bug has crept in (just one!). Find it and neutralize it! Use the IntelliJ IDEA debugger to do this.
Debug, debug, and again debug
- 8
Locked
Comments (29)
- Popular
- New
- Old
You must be signed in to leave a comment
towelie8
28 June 2022, 11:37
When i try to use the debugger in Intellij, it always jumps to a different task. I tried to restart Intellij, reload it.. but it jumps always to an older task :(.
0
John Squirrels Website Admin at CodeGym
28 June 2022, 11:43
There are a few ways how to make the required task compiled without compiling the other code solutions from previous tasks.
1. Press CTRL + SHIFT + F10, when the cursor is active in the tab with the required task code.
2. On the left next to the Main method in the Solution tab of the required task, where the number of lines is indicated, you need to click on the green triangle and choose the upper option which is called like ClassName.main, i.e. ("Run Solution.main CTRL+SHIFT+F10").
3. In the Solution tab of the required task, please, click on the right button on the mouse. Then choose menu option "Run ClassName.main", i.e "Run Solution.main CTRL+SHIFT+F10").
If all of the above does not work, you can resort to more drastic measures:
1. Mark all used tasks as excluded. To do this, left-click on the used task and select Mark Directory as -> excluded in the drop-down menu. The task#### folder will turn orange.
2. If the method is not implemented but should return something, put stubs (return 1, return "", return new HashMap<>());
3. Comment out the problematic code in the previous tasks in order for the code in the required task would compile and wouldn't interfere with other tasks. How to do it? Select the problematic piece of code of the previous task and press CTRL + /. When you return to this task, then simply remove it from the comments.
+3
towelie8
28 June 2022, 11:54
Thanks <3
0
Justin Smith
22 August 2021, 17:46
I think there's a good lesson in this task, but they also kind of shoot themselves in the foot. The task introduces some of Intellij IDEA's debugging processes, but then the conditions literally tell you how to solve the task. If you follow the condition instructions you never have to use the debugger it talks about. #3 of the requirements should probably be removed so that the user has to use the debugger and figure it out.
+2
DarthGizka
4 June 2021, 21:28
A very dark corner indeed. Even though Java talks about 'accessibility' just like C++, its modifiers are actually about *visibility*, not accessibility, just like in Delphi. I.e. the compiler doesn't abort with an 'access denied' error (or 'cannot override private base class method', in the present case); instead it pretends that the private things do not exist at all from the POV of class outsiders.
BTW, Delphi and C# require the override keyword when overriding virtual methods. This makes things a lot safer because it tells the compiler about the *intent*. Should someone take away the base class method (for example, by making an incompatible change to the function signature) then you get a compiler error in Delphi and C#. In Java you don't get a peep from the compiler (although you can get a warning if you use annotations). The same applies for accidental overriding, which is essentially the basis for task1526. Can't happen in Delphi and C#. Enough decades have passed for Java to follow suit, but so far it hasn't. Pity.
And kudos for the task setter! This was not only a very interesting puzzle, it also taught us about a dark corner that we all need to know about.
0
Guadalupe Gagnon
15 November 2021, 15:15
Java can't "follow suit" otherwise decades (as you mentioned) of written code would be instantly obsolete and break. The annotations were added as a way to include a basic rendition of this functionality that wouldn't break that pre-existing code. I am not familiar with Delphi, but C# was designed as a carbon copy of Java using the .NET framework. Since it came out around a decade later it was able to fix/avoid a lot of the problems that Java had in it from either lack of foresight or by just plain old mistake.
0
Guadalupe Gagnon
8 April 2020, 15:28
Some explanation of this task and the lesson that should be taken away from it:
To understand this you have to understand what goes on with object construction. First though all programs are started with the main method, the entry point of an application, which is static and require no objects to be made. Inside the main method for this task there is one line, new B(6);, which is creating a new object of B.
Now from there what happens, starting from the bottom of the inheritance chain, all the objects (parent/child objects) are created with default values, meaning everything is set to '0' for primitives or 'null' for objects. Then the actual objects are created from the very top of the inheritance down. So in this task:
Step1: B is created and all the fields are set to default values
Step2: A is created and set to default values.
Step3: The constructor for A is called and does its work
Step4: The constructor for B is called and does its work
Now with all that, you see that both A and B have an int 'f1' and a method 'initialize'. The B class is over riding those two with its own implementations. Because A is only being created as an object of B, when initialize is called inside its constructor it actually calls the B classes initialize and not the A classes. Now going back to what I said above, at this point B only has default values. So the initialize that is called in the A's constructor, which is over ridden by B's initialize, will call the B's initialize and output the B's f7. Because this is called before the B's constructor it will output the default value of 0. By changing the initialize in A to private that means that B's initialize cannot over ride it and when A's constructor calls initialize it correctly calls the initialize in A and not the one in B.
+53
Guadalupe Gagnon
8 April 2020, 15:29
As per the recommendations in the conditions I recommend setting a break point at line 10 and you can step through the code from there.
+1
Angel Li
12 July 2020, 23:40
Thanks! This helped a lot, although it's still confusing for me.
+1
Sela
24 July 2020, 16:09
very well explained. thank you.
+1
MoJo
1 November 2020, 13:01
Very nice explanation thanks.
I tried in the A class 'this.initialize', i thought this should work too but it still calls b's initalize tho. Can you explain why?
Also setting both classes 'f1' and 'initialize()' to static (but the function is still only protected) outputs '6 9" aswell. Could you give us an insight here too?
Thanks!
0
Guadalupe Gagnon
1 November 2020, 17:00
this points to the current object. If you are still creating an object of B, the current object will still be an instance of B even inside the code for A, so using this won't supersede that. From a parent class there is no keyword to make sure that you only call the parent's method when a child class has over ridden that method, other than breaking the ability of the method to be over ridden by making it private. However, from a child class, you can use the super keyword to call the parent's method instead of the child's as you wish.
+3
Gellert Varga
15 March 2021, 23:46
Guadalupe, I believe in you to the maximum, what's more also the working of the code fully supports your explanation:)
(I'm talking about the order of steps of creating an object, as you wrote it on 8 April 2020: Step1 Step2 Step3 Step4.)
But i wonder why is it that CG lessons explained us a different order?:(
They explained it this way:
1) initializing static variables of the parent class
2) initialization of static variables in a child class
3) initialization of the parent class instance-variables
4) executing the parent class constructor
5) initializing the child class instance-variables
6) executing the child class constructor.
In your opinion what would be the real full order of steps of an object-creation, if the above classes A and B would contain also static members?
+1
Guadalupe Gagnon
16 March 2021, 13:13
@Gellert Right, I didn't mention statics because they are not in this task, but they are indeed initialized first before any of the steps that I said. The codegym steps are the same as mine except that they don't mention how the class fields are initialized to default values. They absolutely have to be or instead of getting 0 as the original output of this code then you would get some sort of initialization error or null pointer exception.
+1
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
16 April 2021, 23:09
All that seems informative, but I solved by just following Requirements...doing that cause the desired result to happen.
0
Douglas
28 July 2022, 22:56
Thank you for your answer @Guadalupe! But I still can't make sense of something.
Why calling this.initialize() when 'this' is a instance of class B, would call the class A initialize?
Doing some tests in the IDE I concluded that:
The 'this', as you have said, is defined by the object type in which we called the method.
So 'this' is 'objectB' all the time.
But it does not determine where the JVM will first look to find the method or variable.
What defines the order in which the JVM will look for a variable or method is the scope defined by { }?
(EDIT: Actually no! it's not the { } but the type of the variable that points to the instance, see the answer below)
Also, look at the picture below:
here is a strange thing:
When we print the 'this', it's a object of the class B like you said.
But on the line below, when trying to print the variable 'this.f1', we get the f1 of the parent!
How does this work??

0
Douglas
28 July 2022, 23:41
I found how it works.
What defines the method or variable that will be called is not the instance type.
It's the type of the variable that points to the instance.
As we saw in previous lessons, every method has a 'this' as a secret parameter.
When we call a method, or a constructor, the instance of the object in which we called the method is passed as a hidden 'this'.
For an example:
`A a = new A();
a.print();`
is actually:
A a = new A();
A.print(a);
And:
class A {
A () {
initialize(); // here I didn't understand why we called the initialize from A
// as the hidden 'this' was an instance of B
}
}
It's because the hidden 'this' inside the constructor of A is of the type 'A'.
class A {
A (A this) {
this.initialize(); // the initialize of A will be called, even though it points to a instance of B
// because it's the type of the pointer that determines what method will be
// called.
}
}
As the 'this' is hidden, we can't cast it to a variable of the type B to access the child method.
The only way is to override the parent method in the child class, so that when the parent method is called, the child method will get called instead.
Above I defined f1 in the parent and child classes.
I then tried to access f1 in an instance of the child.
Using a pointer of the type of the parent, I get the parent value;
Casting to a child type, we get the child value.
read more here
https://stackoverflow.com/questions/4595512/java-calling-a-super-method-which-calls-an-ove

0
Douglas
29 July 2022, 00:01
So the reason we initially were getting the class B initializer is because the JVM was looking for the initializer of the class A, but as it was overridden, it would proceed to get the class B initialize().
It's not because the 'this' is an instance of B!
It was looking first at class A because the 'this' gets casted to a class A pointer when entering the constructor of A (the object pointed at is still of class B, but the variable that points to it is of the class A).
When we blocked the override by making the class A initialize() private, when the JVM goes to the initialize() of the class A it stops there.
There's nothing saying to it: "hey JVM, go look in class B because I am overridden".
So then it is able to execute the class A initialize().
0
Guadalupe Gagnon
29 July 2022, 13:58
I think your confusion is that methods with the same name and parameters are overridden and no matter what parent class called the method only the actual method in the class of the object type will be called.
Variables do not act like this. When you have common heritage classes (one inherits the next to create a chain) that have the same variable name you can only access the variable in the current class code or the variable in the very next parent class: Take this example:
When you run it you can see that in the C.print() method only "B" and "C" are printed. In the B.print() method only "A" and "C" are printed. And finally in the A.print() method only "A" is printed. This is all despite the actual object being a C object.
From the C class code you just can't print the A class method. Trying to print super.super.x is invalid. you can only print "C" or "B" even though a C object is a B AND A object as well. From the A class code you just can't directly print the C.x (or B.x) variable even though the object is a C object. You could do it by using an overridden method, just not directly. 0
Guadalupe Gagnon
29 July 2022, 14:14
And i think you got the right idea with the 'private' keyword. When a method is 'private' it CAN NOT be over ridden. You can still have the same method in a child class, but it will not over rider the parent method.
You might have seen "@Override" on some methods here and there in code. This is a preprocessor tag where the IDE will check that the method marked with it actually over rides a parent method. If it does not then an pre-compile error will occur and you will not be able to run the code. This was added because if a programmer made a method expecting it to over ride a parent method and it did not then there would have been a HUGE bug in the code and it was very hard to find. This prevents that. Example:
This code is legitimate and will compile and run (though what it does is not impressive at all). If you were to try adding the @Override tag on A.method1() then the code will no longer compile because A.method1() does not over ride the parent classes method1() and cant because method1() is private in the parent class. 0
Davedabull
20 February 2020, 04:45
https://stackoverflow.com/questions/1387207/can-a-parent-call-child-class-methods
this question is so odd
+1
Thomas Sixberry
15 February 2020, 18:26
I mean it is cool and all that these are out here to help with learning intellij but the only problem I have is the past couple of tasks I don't have finished it keeps pulling those up first. So it makes it impossible to learn or do anything with this program full of barriers.
0
MaGaby2280
16 January 2020, 16:04
Could someone help me out with this task, I have tried changing the access modifier but it stil won´t verify!! Help please!!!
0
Alex Vypirailenko Java Developer at Toshiba Global Comme
17 January 2020, 07:20
Please create a question with your code in the Help section.
+1
MaGaby2280
20 January 2020, 16:18
Thank you! I did so... finally was able to pass the test!
0
Peerawas
1 January 2019, 14:12
Why initialize(); in A's constructor calls initialize(); in B class ??
+4
Ahmed
31 May 2019, 16:11
I didn't understand this one either. Why is the method initialise in the super class is calling the one in the subclass. If someone has a clue please till us.
+2
yz Backend Developer
1 November 2019, 05:26
whenever u create a object which inherits other class it creates parent object too
0
JianQiu Hwang
29 February 2020, 09:34
Java has no memory for `super`. When the code run to `initialize()` of A, the superclass, that code is as same as `this.initialize()`. And the `this` is pointing to B, the subclass.
For more answers, visit The same question link
+2