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