Question: Close any open files and now open ClassB.java. Look at the two variables defined at the top and notice y has the word static in

Close any open files and now open ClassB.java. Look at the two variables defined at the
top and notice y has the word static in its definition. This means y is static but x is not.
2. In the main method, add two lines to initialize both x and y to have values of 10, i.e.
x =10;
y =10;
3. You should see that the first of those two lines shows a compiler error. This is because x
is non-static and we are trying to access it in a static method. Basically the problem is,
there is no indication of which x should be given a value of 10. Non-static variables have
to be accessed on objects rather than in a static way like this.
4. Before the line x =10;, create an object of ClassB using the following line:
ClassB obj = new ClassB();
5. Now change the offending line so refer to the x variable on the obj object:
obj.x =10;
6. Now it should work. This is because we are telling it which x to change to 10. It is the x in
the obj object.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!