About Question enthuware.ocajp.i.v7.2.1116 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1116 :
Hi all, I don't get why isn't correct the answer B and D, the instance - static member of a class aren't initialized by default constructor?
How can you see the definition of the default constructor if it isn't defined in the code? Should I decompile the class file?
Best regards
How can you see the definition of the default constructor if it isn't defined in the code? Should I decompile the class file?
Best regards
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1116 :
Yes, you can decompile the byte code and see that the default constructor exists and what it does. Actually, it does nothing.
It is basically same as:
ClassName(){
}
HTH,
Paul.
It is basically same as:
ClassName(){
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Thu Feb 28, 2013 9:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1116 :
In my opinion the answer:
It initializes the instance members of the class.
It's correct.
The default constructor (and all kind of constructor) initialize a instance member.
Right?
It initializes the instance members of the class.
It's correct.
The default constructor (and all kind of constructor) initialize a instance member.
Right?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1116 :
No, it is not correct. The default constructor provided by the compiler does not initialize any thing. You can write your own constructor to initialize the members.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Apr 30, 2013 8:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1116 :
I can't see why muttley is wrong - doesn't the default constructor initialise the instance variables of the class to their default values - thus B being correct?
If the default constructor doesn't initialise uninitialised variables to their default values, then what does?
kind regards - real exam in 3 days...
If the default constructor doesn't initialise uninitialised variables to their default values, then what does?
kind regards - real exam in 3 days...
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1116 :
The default values are provided even before the constructor is executed. These values are given when the memory is initialized for the object. The following code proves that the default value is given before the constructor is executed:
Output:
HTH,
Paul.
Code: Select all
class A {
int i = 10;
public A() {
System.out.println("In A's Constructor, A's i = " + i);
printValue();
}
//This is not invoked for an object of class B
public void printValue() {
System.out.println("In A's printValue : A's i " + i);
}
}
public class B extends A {
int i = 20;
public B() {
i = 30; //B's constructor sets i to 30
System.out.println("In B's Constructor, B's i = " + i);
}
//override A's printValue
//this is called from A's constructor even before B's constructor is invoked.
//Prints 0 because B's constructor hasn't even been called yet so i still has its default value assigned by the JVM
public void printValue() {
System.out.println("In B's printValue B's i = " + i);
}
public static void main(String[] args) {
B b = new B();
}
}
Code: Select all
In A's Constructor, A's i = 10
In B's printValue B's i = 0
In B's Constructor, B's i = 30
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Dec 23, 2015 11:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1116 :
The easiest way to look at this question would be to recall the order of initialization of blocks of an class.
1) static variables and static blocks according to the order they appear on the class
2) instance variables and instance blocks according to the order they appear on the class
3) constructor
So, instance variable have already been initialized to their defaults before constructor get calls.
Answer B and D are incorrect. Because we could re-initialize instance fields, but that is not constructor's responsibility always.
1) static variables and static blocks according to the order they appear on the class
2) instance variables and instance blocks according to the order they appear on the class
3) constructor
So, instance variable have already been initialized to their defaults before constructor get calls.
Answer B and D are incorrect. Because we could re-initialize instance fields, but that is not constructor's responsibility always.
Who is online
Users browsing this forum: No registered users and 53 guests