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

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

Moderator: admin

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

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

Post by admin »

Yes, static methods work the same way.
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

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

Post by baichen7788 »

Even though I chose the correct option, I could not understand why j=30.
j=30 makes no sense.
Is j initialised in static block or outside of static block?

If j is initialised in the static block, then j=100 should be the answer because instance variable is executed after static block.
If j is initialised outside of static block with j=100, then static block is executed after the instance variable initialisation, which is contradiction with the rule.

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

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

Post by admin »

j is an instance field. Why do you think it might be initialized in a static block? No where in the given code is j accessed in a static block!

You might want to go through the code again more carefully.
If you like our products and services, please help us by posting your review here.

Syams123
Posts: 3
Joined: Sun Sep 26, 2021 5:12 pm
Contact:

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

Post by Syams123 »

Hi,

So can we initialize static variables in instance block? Code will compile without error?

Thanks

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

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

Post by admin »

static and instance fields are always automatically initialized by the jvm to their default values at the place where they are declared if you don't specify an explicit value at the time of declaration.

You can certainly assign a new value to a static field in an instance block.

You may write a short test program to try it out.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 35 guests