Page 1 of 1
About Question enthuware.ocpjp.v7.2.1111 :
Posted: Fri Jan 04, 2013 9:56 am
by Ryhor
Who can explain why the code doesn't need to have a constructor without parameters in class B to be compiled without errors?
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Fri Jan 04, 2013 11:53 am
by admin
Class B does need a no-args constructor because the constructors of class C depend on that. But since class B does not define any constructor explicitly, the compiler provides the no args constructor to class B automatically.
As the explanation says, "Because //B1 is not a constructor. Note that it is returning an int."
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Fri Jan 04, 2013 2:58 pm
by Ryhor
Yeah! How I couldn't notice that! Class B does not provide any constructor explicitly...
Thanks!
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sat Dec 14, 2013 1:08 pm
by petlju
"One of the constructors of each class is called as a result of constructing an object of class C.
To create any object one and only one constructor of that class and each of the super classes is called."
But how that does work with constructor overloading and this();?
Is it then not correct to say that two constructors were called for that class? One explicitly and one implicitly?
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sat Dec 14, 2013 10:10 pm
by admin
Calling this(...) is not much different from calling any method in a constructor. The explanation is talking about constructors that are called by the JVM as a result of doing new on a Class. The JVM calls exactly one constructor.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Fri Jun 27, 2014 4:29 pm
by sarakh
The code says
Code: Select all
boolean b1 = false;
boolean b2 = false;
if (b2 = b1 == false){ System.out.println("true"); }
else{ System.out.println("false"); }
And the explanation says
All that if() needs is a boolean, now b1 == false returns true, which is a boolean and since b2 = true is an expression and every expression has a return value (which is the Left Hand Side of the expression), it returns true, which is again a boolean. FYI: the return value of expression i = 10; is 10 (an int).
So the order of execution is:
1. b1 == false
2. b2 = result of 1
we also have
enthuware.ocajp.i.v7.2.949
with code
Code: Select all
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2){
System.out.println("true");
}
else{
System.out.println("false");
}
and explanation
Note that boolean operators have more precedence than =. (In fact, = has least precedence of all operators.)
so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegal because false is a value and not a variable!
So the order of execution is:
1. b2 != b1
2. result of 1 = !b2
So if I want to talk about the order of evaluation of =, != and ==, can I say:
- first != or == is evaluated, whichever comes first in the code
- then = is evaluated.
Is that correct?
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Fri Jun 27, 2014 8:43 pm
by admin
Yes, that is correct.
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sun Jan 01, 2017 8:07 am
by jagoneye
I was surprised to see the Code compilation fails to be incorrect only to find later that
the line
public int B(String s) { System.out.println("B :"+s); return 0; } // B1
is valid!! I think you should also add the explanation for this line too! Since it isn't read the following link:
http://stackoverflow.com/questions/3401 ... ructor-why
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sun Jan 01, 2017 11:15 am
by admin
It does include an explanation under this option that tells that it is valid method and not a constructor.
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sun Jan 08, 2017 3:43 am
by jagoneye
admin wrote:It does include an explanation under this option that tells that it is valid method and not a constructor.
Yes it does. But it doesn't say that such methods are allowed.
Also the below code also compiles which is suprising to me:
public B()
{
}
public int B() { System.out.println("B :"); return 0; } // B1
}
Great question btw!
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sun Jan 08, 2017 6:43 am
by admin
If something is valid doesn't that mean it is allowed? It is allowed that is why it is valid, no?
Re: About Question enthuware.ocpjp.v7.2.1111 :
Posted: Sun Jan 08, 2017 6:48 am
by jagoneye
admin wrote:If something is valid doesn't that mean it is allowed? It is allowed that is why it is valid, no?
True I was lost somewhere during this test I guess to not follow the implications.
