anonymous
  • anonymous
public class StaticChecker { public static void main(String[] args) { sChecker s1 = new sChecker(); } public class sChecker{ static int i =99; } } what is wrong with this piece of code?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
@rsmith6559 @slotema
anonymous
  • anonymous
Java does not seem to like the use of an inner class in a static function. If you put the sChecker class in a separate file, it should work.
anonymous
  • anonymous
yes that worked but the value of i is changed for both the objects :-/ public static void main(String[] args) { sChecker s1 = new sChecker(); sChecker s2 = new sChecker(); System.out.println(s1.i); System.out.println(s2.i++); System.out.println(s1.i); } Result:- 99 99 100 i am able to manipulate a class using an object !!!

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
The fact that `i` is updated for both objects is normal. A static variable/method is independant of any object. It is a shared variable that can be accessed even without an object (as long as it's declared public. You can also access it with `sChecker.i`.
anonymous
  • anonymous
yup i did that too

Looking for something else?

Not the answer you are looking for? Search for more explanations.