Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the output of this program? Please Explain?

 
Gautham Muralidharan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Source of this question: http://javasehelp.blogspot.in/2011/12/scjp-questions-201-210.html

Thank You in advance.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this first --->Pass-by-Value Please

  • item is a method parameter variable of static method modifyDesc whose type is Item. The method parameter effectively ceases to exist when the execution of the body of the method is complete.
  • First method parameter item was referring to the object referred by it variable because It's reference was passed as argument while invokng this method from main, here


  • In method modifyDesc, new object of class Item is created whose reference is assigned to item variable. Now item variable is referring to this new object rather than object referred by it variable.
  • Method setDescription is invoked on this new object of Item referred by item variable. This method has String argument desc i.e. "Scrumdiddlyumptious" which sets value of instance variable desc of this newly created object to "Scrumdiddlyumptious".
  • Method getDescription() which returns the value of instance variable desc of this newly created object of Item and prints "Scrumdiddlyumptious" here.


  • If you still have doubts please rename these reference variables and post code again. It is so confusing because class name is Item and reference variable name is also item. Rather than it, it2 write obj1 , obj2 like that
    Did you run the code ? what output did you get ?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Gautham Muralidharan wrote:What is the output of this program? Please Explain?

    Remember that, in Java, everything is pass by value. This is a very important concept in Java. So a good solid understanding is required to pass the exam. So you'll probably want to read this excellent article as well. And in this topic you'll find an excellent explanation (with code examples) about this concept too. So definitely worth reading! And if you would still have doubts or questions, don't be shy and just hit the reply button

    Hope it helps!
    Kind regards,
    Roel
     
    Gautham Muralidharan
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Scrumdiddlyumptious
    Gobstopper
    Fizzylifting

    This is the output I got.
    Gautham
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes that is correct output. Did you understand how you got that output? I assume you have gone through that article and topic link Roel provided you.
     
    Gautham Muralidharan
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank You Ganesh and Roel.
    Now I have understood it clearly. Thank you both.
    Roel's links were useful for me to learn pass by value.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic