class BooleanTest{
public static void main(String[]args){
int a;
boolean m;
System.out.println(a);
System.out.println(m);
}
}
Rather than giving default values of boolean (false) and integer (0) its asking for initialization.
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
27 October 2020, 22:58
correct?
0
Dinesh
27 October 2020, 23:35
Why its asking for initialization if default constructor is being called and its work is to initialize the values . It should take the default values automatically. Am I correct?
0
Guadalupe Gagnon
28 October 2020, 13:37
You are confusing class variables (those that are part of the class and outside of any methods like main) and local variables. They will be set to default values when the class code is originally loaded into the JVM and then set to any values first on the line declared, then by the constructor. Take this little bit of code as an example (this gets a little ahead of where you are but i'll try to keep it simple):
The code is going to start in the main method where a Test object is created. This is the series of events:
#1 Test class is loaded into memory, all class variables are given default values
#2 Solution class, being the parent of Test, is loaded into memory and class variables are given default values
#3 Some other technical things related to OOP happen, will skip this step
#4 Solution class variables that have initializations defined are set (line 2 where A is set to 5)
#5 Solution class constructor is called. In this code it outputs 'A' and 'TestA'
#6 Test class variables that are defined are now set
#7 the Test class constructor is called
I used this example because the output is going to be:
Solution constructor reached
5
printTestA 'test' class
0 // this line is the output of testA
Test constructor reached 0
Guadalupe Gagnon
28 October 2020, 13:37
If you notice the output of 'testA' is 0 but line 19 sets its value to 15. The reason it is outputting 0 is if you follow my numbered events, the values in the Test class are not set beyond the default before the Solution class constructor (which calls the method that outputs the testA value) is called. If Java didn't set default values first then this code would throw an error. With large projects with multiple levels of inheritance and methods being overridden, this becomes an extremely important subject to understand, however at your level I wouldn't worry too much about this yet as you have a lot of things to learn prior to grasping this. Keep it up though, you're question here was a good one. I hope you can get some information from my post without being over burdened with too much information.
0
Dinesh
28 October 2020, 20:08
Thank you so much for taking pain to make me understand this stuff.
You have tried to make me understand the stuff the best possible way.
Though It is not my level that I may understand all this stuff in one go.
But going through the series of events I want to try and explain my understanding about the
Events of the given code one by one.
Please check and help me wherever I am wrong in understanding the same.
#1 Test class is loaded into memory, all class variables are given default values.
It means as soon as the execution of the program from the main method will start
line no 10 object --> (test) will help in loading the class
class Test extends Solution
and there is only one class variable in this, that is intTest A and it has been assigned value 15
so default value that is 0 will not be assigned to it.
AM I Right?
0
Guadalupe Gagnon
28 October 2020, 20:29
The default value on line 19 will be assigned to testA during construction of the object on line 10. After the object finishes construction, testA will be assigned the value 15 as the code says it should.
With objects, the child classes starting from the bottom are pre-loaded into, memory meaning that all the values are set to default values, starting from the very bottom child class until it reaches the top parent class. Then, starting from the, the parent class loads fully (where values are properly set and the constructor is run), followed by the child class beneath it, all the way down to the bottom. So If I had 4 classes where class1 was the top, then class2 extended class1, then class three extended class3, and finally class4 extended class3. So it would look like this:
class1 < class2 < class3 < class4
If you created an object of class4 then starting from the bottom:
- class4 would be preloaded
- class3 would be preloaded
- class2 would be preloaded
- class1 would be preloaded
At this point of construction ALL variables have default values. Then starting from the top
- class1 would finish loading and constructor called
- class2 would finish loading and constructor called
- class3 would finish loading and constructor called
- class4 would finish loading and constructor called
After this finishes, all the variables that have values assigned will have the proper values.
0
Dinesh
29 October 2020, 07:35
Thank you for unlocking the Potential of java with your kind efforts to make me understand the following stuff:-
1. How class loaded in the memory in hierarchy when inheritance concept used.
2. How default values assigned to the variables during object creation.
3. How proper values assigned in place of default values to the variables that have some value.
0