Page 1 of 1

About Question enthuware.ocajp.i.v7.2.936 :

Posted: Thu Sep 19, 2013 3:36 am
by TheSarjo
Hello!
This question made me reflect a bit, so i tried this little program. But.. the result is not what i was expecting (based on the explanation given in the answer of the exercise).
Here is my code:

Code: Select all

class AClass
{
	AClass(){System.out.println("initialized AClass");}
	 static void method(){System.out.println("AClass method");}
}

class Prove1 
{
	Prove1(){System.out.println("initialized Prove1");}
	 
 	 public static void main(String[] args)
 	 {
 	 	 Prove1 p = new Prove1();
 	 	 AClass.method();
 	 	  	 }
}
Now, given that
As per JLS 12.4.1 - A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
...
T is a class and a static method declared by T is invoked...
I thought that invoking AClass.method() would create a new AClass instance, and so the AClass constructor would be called. But it's not so.
So, i don't understand the explanation given. :-)

p.s. the result is:
initialized Prove1
Base method

Re: About Question enthuware.ocajp.i.v7.2.936 :

Posted: Thu Sep 19, 2013 8:03 am
by admin
Why would invoking AClass.method() create a new AClass instance? Since the method is static, no instance is required.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.936 :

Posted: Tue Jun 17, 2014 2:52 am
by mmontelatici
Hello I cannot completely understand the following sentence about JLS on class initialization:
"T is a top-level class, and an assert statement lexically nested within T is executed."

Can someone explain o example it?

Re: About Question enthuware.ocajp.i.v7.2.936 :

Posted: Tue Jun 17, 2014 5:07 am
by admin
It is difficult to understand the meaning just from one isolated sentence. You need to read the complete subsection in its entirety.

Re: About Question enthuware.ocajp.i.v7.2.936 :

Posted: Wed Aug 05, 2015 3:49 am
by Macadoshis
Why the answer is not as follows : ?????

It will print super two.

Since when do the following instructions print out "super and two" ???

Code: Select all

System.out.println("super ");
System.out.println("two ");
I wanted to be rigorous, so I checked answer None of the above, and as a reward I won a wrong answer... that's not fair.

Please stick to your outputs !!!! or otherwise modify your instructions as :

Code: Select all

System.out.println("super and ");
...
System.out.println("two");

Re: About Question enthuware.ocajp.i.v7.2.936 :

Posted: Wed Aug 05, 2015 4:16 am
by admin
If you look at the options carefully, they list each word of the proposed output in monospace/code font. The correct option does not say it will print "super and two". It says it will print super and two, where "super" and "two" are in different font while "and" is not. Other options follow the same pattern.

So while I understand that you got mislead but the option is correct. Even so, your point is valid and I am updating the options to avoid this confusion altogether.

HTH,
Paul.