Answered step by step
Verified Expert Solution
Link Copied!

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.
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

Explain why it is important to measure customer retention.

Answered: 1 week ago

Question

LO2 Compare three types of individual incentives.

Answered: 1 week ago