Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Mon Oct 29, 2012 3:53 pm
by ETS User
This explanation does not seem to clear the topic at all, particularly, why "In Sub" wont print???

Re: About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Mon Oct 29, 2012 4:12 pm
by admin
In Sub is not printed because Sub is never initialized. That is what the explanation is referring to.
But I agree that the explanation should be made clearer.
HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Mon Oct 29, 2012 4:41 pm
by same
Paul, yes, that's still a question, why its not initialized if at many other questions it clearly shown that static's stuff loaded first, even before contstructors initialization?

Re: About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Mon Oct 29, 2012 4:52 pm
by admin
same wrote:Paul, yes, that's still a question, why its not initialized if at many other questions it clearly shown that static's stuff loaded first, even before contstructors initialization?
Again, as the explanation says, Sub is not initialized because it is not actively used. The explanation describes what consitutes active use and passive use.

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Mon Oct 29, 2012 4:56 pm
by Guest
admin wrote:
same wrote:Paul, yes, that's still a question, why its not initialized if at many other questions it clearly shown that static's stuff loaded first, even before contstructors initialization?
Again, as the explanation says, Sub is not initialized because it is not actively used. The explanation describes what consitutes active use and passive use.

HTH,
Paul.
yeah... well would be better if it would be reworded a little clearer as i am not able to understand the case which it will be applicable to

Re: About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Sat Jul 24, 2021 4:51 am
by tupan93
Hi.
I have a question about this point:
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
A static method declared by T is invoked.

Here I call the static method callme() from Sub, it does print "In Sub" but does not call the constructor.

public class Questions{
public static void main(String[] args){
Sub.callme();
System.out.println(Sub.ID);
}
}

class Super {
Super(){
System.out.println("Why Super Contructor was not called?");
}
static String ID = "QBANK";
}
class Sub extends Super{
Sub(){
System.out.println("Why Sub Contructor was not called?");
}
static { System.out.println("In Sub"); }
static void callme(){}
}

Thank you.
Ricardo.

Re: About Question com.enthuware.ets.scjp.v6.2.491 :

Posted: Sat Jul 24, 2021 11:09 pm
by admin
Constructor (and instance initializers) are called only when an instance is created. If you are not creating an instance then there is no reason to invoke the constructor (and instance initializers). If you think about it, constructors (and instance initializers) are invoked on an instance. If there is no instance, on what object will you invoke them?

>A class or interface type T will be initialized...
Class initialization is not the same as instance initialization. This is not about an instance of that class. It is about the instance of the Class class that is used to represent the class.