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

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

Moderator: admin

Post Reply
TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post 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

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

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

Post by admin »

Why would invoking AClass.method() create a new AClass instance? Since the method is static, no instance is required.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

mmontelatici
Posts: 2
Joined: Wed Jan 22, 2014 4:24 am
Contact:

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

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

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

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

Post by admin »

It is difficult to understand the meaning just from one isolated sentence. You need to read the complete subsection in its entirety.
If you like our products and services, please help us by posting your review here.

Macadoshis
Posts: 1
Joined: Wed Aug 05, 2015 3:41 am
Contact:

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

Post 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");

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

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

Post 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.
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 7 guests