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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

This explanation does not seem to clear the topic at all, particularly, why "In Sub" wont print???

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

same

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

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

Post 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

tupan93
Posts: 1
Joined: Tue Mar 30, 2021 2:36 am
Contact:

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

Post 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.
Last edited by tupan93 on Mon Jul 26, 2021 2:53 am, edited 1 time in total.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 76 guests