Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started