Page 2 of 2

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

Posted: Thu Aug 27, 2020 2:12 am
by admin
Yes, static methods work the same way.

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

Posted: Mon Jun 07, 2021 11:14 am
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.

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

Posted: Mon Jun 07, 2021 12:18 pm
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.

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

Posted: Sat Oct 09, 2021 11:15 am
by Syams123
Hi,

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

Thanks

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

Posted: Sat Oct 09, 2021 8:41 pm
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.