This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

please explain the code

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic