Question: Consider the following class definitions. public class Class1 { private int val1; public Class1() { val1 = 1; }public void init () { Class2 c2
Consider the following class definitions. public class Class1
{ private int val1; public Class1() { val1 = 1; }public void init ()
{ Class2 c2 = new Class2(); } c2.init(this, val1); public void update(int x) { val1 -= x; }public int getVal()
{ return val1; }}
public class Class2
{ private int val2; public Class2() { val2 = 2; }public void init(Class1 c, int y)
{ c.update(val2 + y); } }
The following code segment appears in a method in a class other than Class1 or Class2. Class1 c = new Class1(); c.init(); System.out.println(c.getVal());
What, if anything, is printed as a result of executing the code segment?
A. 2 B. 1 C. 0 D. -2 E. Nothing is printed because the code segment does not compile.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
