Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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
See more answers at brainly.com
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.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
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.
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 !!!

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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`.
yup i did that too

Not the answer you are looking for?

Search for more explanations.

Ask your own question