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.
In the main method, add two lines to initialize both x and y to have values of ie
x ;
y ;
You should see that the first of those two lines shows a compiler error. This is because x
is nonstatic 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 Nonstatic variables have
to be accessed on objects rather than in a static way like this.
Before the line x ; create an object of ClassB using the following line:
ClassB obj new ClassB;
Now change the offending line so refer to the x variable on the obj object:
obj.x ;
Now it should work. This is because we are telling it which x to change to 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
