• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

please explain the code

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hii everyone...Can anyone please explain me the following code line by line..as what is happening in everyline i'll really appreicate...thanks

1.class Q6{
2.public static void main(String args[]){
3.Holder h= new Holder();
4.h.held=100;
5.h.bump(h);
6.System.out.println(h.held);
7.}
8.}
9.class Holder{
10.public int held;
11.public void bump(Holder theHolder){
12.theHolder.held++;}
13.}

output is - 101
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do these comments help? What exactly are you questioning here?
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hii marc thanks for the explaination can you please explain me the line of class Holder.. Particularly line 11 and 12 .

9.class Holder{
10.public int held;
11.public void bump(Holder theHolder){
12.theHolder.held++;}
13.}
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a definition for a method called bump. The method has public access and its return type is void (meaning that it doesn't return anything).

The method takes an instance of Holder as an argument. Inside the method, that instance of Holder is referenced by a local variable called theHolder.

In the method body (inside the {} braces), the held variable of "theHolder" is incremented by 1. This is done using the post-increment operator ++.

There is no value returned by this method, but because it's using a reference to an instance of Holder, that instance of holder will be modified.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
9.class Holder{
10.public int held;
//Method Receives an instance of Holder. The Holder instance is passed by references
11.public void bump(Holder theHolder)
{
//Increments the held property of the Holder instance
12. theHolder.held++;}
13. }
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Patrick Patel:
... The Holder instance is passed by references...


Careful... Java is pass by value, so a copy of the reference is passed.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic