programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How does static change the code?

Cj Hooper
Greenhorn
Posts: 5
Hello!
I recently took a quiz and I was hoping someone could explain the correct answer to me. I have researched the question but still believe my answer was correct.

The question is:

The Test class is given below. Assume that ann and sue are variables of this Test class, where y is a static variable but x is not.. What number is printed by the sequence of 3 statements ann.think(); sue.think(); ann.print(); right after ann and sue are initialized?

I had chosen 36 as the answer. The quiz states the answer is 42. Can anybody please explain why? Thank you so much!

Liutauras Vilda
Sheriff
Posts: 4917
334
the sequence of 3 statements ann.think(); sue.think(); ann.print();

where y is a static variable but x is not

Answer above. Try to figure it out.
Whats coming up to your mind?

J. Kevin Robbins
Bartender
Posts: 1801
28
Static means that there is only one copy per class, not per object. So no matter how many test objects are created, they all share the same "y" variable, but each gets it's own "x" variable. Think of it as a global variable.

Now, try the math again.

Junilu Lacar
Sheriff
Posts: 11490
180

Cj Hooper
Greenhorn
Posts: 5
Thank you so much! I am really struggling with this course. Is this correct?

Junilu Lacar
Sheriff
Posts: 11490
180
Not quite. Ann and Sue each have their own x -- so when Ann's x changes, Sue's x doesn't, and vice versa. Since y is static, it means that there's only one of it shared between the entire population of Test instances, which in this case is just Ann and Sue. So when Ann changes y, Sue "sees" that change, and vice versa.

Think of x as a secret that Ann and Sue keep to themselves. It's like they picked a card from a deck and they each have their own card. If Ann folds her card in half, that doesn't affect Sue's card. And if Ann picks another card from the deck, Sue's card (her x) doesn't change. Same thing works the other way around, if Sue changed her card, Ann's card doesn't change.

y on the other hand is a card that the teacher is holding up so both Sue and Ann can see it. If the teacher changes the card she holds up, both Ann and Sue see the same card.

Now, given that, go back and show us how you think those values change again.

Junilu Lacar
Sheriff
Posts: 11490
180
I don't know how a Teacher snuck into that example. You'd think that it would be a Magician who was carrying a deck of cards around and having people pick out cards from it. I guess it's because Teachers can generally be trusted whereas Magicians are tricky

Junilu Lacar
Sheriff
Posts: 11490
180
It probably would be clearer if I gave you this to fill out in the first place:

Junilu Lacar
Sheriff
Posts: 11490
180
And Welcome to the Ranch!

Cj Hooper
Greenhorn
Posts: 5
Thank you so much for clearing that up! It really helped me understand! Have a great day!