Question: The following Java class does something rather peculiar; in line (A) it sets its data member y equal to the value of the data member

The following Java class does something rather "peculiar"; in line (A) it sets its data member y equal to the value of the data member x. The class comes with two constructors, in lines (B) and (C). The constructor in line (B) supplies a value for each of the two data members of the class. On the other hand, the constructor in line (C) initializes only the data member x. What will be printed out by the statements in lines (D) and (E)? [Note: You'll probably be surprised by the correct answer to the question, which you can find out by compiling and running the program.]

class X { private int x; private int y = x; //(A) X( int xx, int yy ) { x = xx; y = yy; } //(B) X( int xx ) { x = xx; } //(C) public String toString() { return " " + x + " " + y; }; public static void main( String[] args ) { X xobj = new X( 100, 200 ); System.out.println( xobj ); //(D) xobj = new X( 300 ); System.out.println( xobj ); //(E) } }
 

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!