About Question enthuware.ocajp.i.v7.2.1251 :
Moderator: admin
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
Yes, static methods work the same way.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
-
- Posts: 3
- Joined: Sun Sep 26, 2021 5:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
Hi,
So can we initialize static variables in instance block? Code will compile without error?
Thanks
So can we initialize static variables in instance block? Code will compile without error?
Thanks
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
Who is online
Users browsing this forum: No registered users and 46 guests