Hi Could you please explain why is this output public class
Hi!
Could you please explain why is this output?
public class TestTables
 {
    public static void main(String [] args)
    {
       Table kitchen = new Table();
       Table den = new Table();
       Table study = new Table();
       Table library = study;
 
       kitchen.chairs = 2;
       library.chairs = 1;
 
       study.chairs += 10;
       library.chairs += 20;
 
       study.print();
       library.print();
    }
 }
 
 class Table
 {
    int chairs = 5;
 
    public void print()
    {
       System.out.println(chairs);
    }
 }
Answer: 31 31
Solution
1.) Table study = new Table();
 This line allocates memory for a Table object, and that location is pointed to by \'study\'.
 Right now, chairs variable of the object stores 1.
2 .) Table library= study;
 This line would Not create a new object. Instead, it would make library instance point to the same location as study .
 So now, library and study are referencing the same object.
3.) library.chairs=1;
 It changes the value of chairs variable in the object to 1.
4.) study.chairs += 10;
 library.chairs += 20;
 It increments the value of chairs variable in the object, first by 10 and then by 20. 1+10+20. Hence, 31.
![Hi! Could you please explain why is this output? public class TestTables { public static void main(String [] args) { Table kitchen = new Table(); Table den = ne Hi! Could you please explain why is this output? public class TestTables { public static void main(String [] args) { Table kitchen = new Table(); Table den = ne](/WebImages/21/hi-could-you-please-explain-why-is-this-output-public-class-1049086-1761546152-0.webp)
