What is the result?

<code>
Class StaticField {
static int i = 7;
public static void main(String[] args) {
StaticFied obj = new StaticField();
obj.i++;
StaticField.i++;
obj.i++;
System.out.println(StaticField.i + ” “+ obj.i);
}
</code>
What is the result?


Class StaticField {
static int i = 7;
public static void main(String[] args) {
StaticFied obj = new StaticField();
obj.i++;
StaticField.i++;
obj.i++;
System.out.println(StaticField.i + " "+ obj.i);
}

What is the result?

A.
10 10

B.
8 9

C.
9 8

D.
7 10



Leave a Reply 2

Your email address will not be published. Required fields are marked *


sully

sully

so static field and obj are the same thing
confused about the variable though doesn’t static mean i can’t be changed.
or is that just for final

Oene Bakker

Oene Bakker

Answer is A

But in Eclipse you will get some warnings because you should access static fields in a static way (i.e. StaticField.i).