SlideShare a Scribd company logo
1 of 2
Download to read offline
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.

More Related Content

More from arihantmobileselepun

A geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdfA geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
arihantmobileselepun
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
aphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdfaphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdf
arihantmobileselepun
 
Who is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdfWho is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdf
arihantmobileselepun
 
Provide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdfProvide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdf
arihantmobileselepun
 
Write a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdfWrite a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdf
arihantmobileselepun
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdfwhy nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
arihantmobileselepun
 

More from arihantmobileselepun (20)

Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdfGerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
 
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdfFor Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
 
Find and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdfFind and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdf
 
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdfA geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
 
Assume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdfAssume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdf
 
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdfDeclining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
aphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdfaphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdf
 
A population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdfA population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdf
 
Who is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdfWho is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdf
 
Provide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdfProvide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdf
 
Write a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdfWrite a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdf
 
Write the interval notation for the set of numbers graphed. Solu.pdf
Write the interval notation for the set of numbers graphed.  Solu.pdfWrite the interval notation for the set of numbers graphed.  Solu.pdf
Write the interval notation for the set of numbers graphed. Solu.pdf
 
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdfHyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
 
Why doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdfWhy doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdf
 
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
 
Which of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdfWhich of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdf
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdfwhy nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
 
what will happen to the photon if two photons enter the same atom si.pdf
what will happen to the photon if two photons enter the same atom si.pdfwhat will happen to the photon if two photons enter the same atom si.pdf
what will happen to the photon if two photons enter the same atom si.pdf
 
Which of the following is not a function of the skeletonA. Storag.pdf
Which of the following is not a function of the skeletonA. Storag.pdfWhich of the following is not a function of the skeletonA. Storag.pdf
Which of the following is not a function of the skeletonA. Storag.pdf
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

Hi!Could you please explain why is this outputpublic class Test.pdf

  • 1. 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
  • 2. 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.