aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  please explain the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " please explain the code" Watch " please explain the code" New topic
Author

please explain the code

anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343


Do these comments help? What exactly are you questioning here?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
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

Joined: Aug 31, 2004
Posts: 11343


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.
Patrick Patel
Greenhorn

Joined: Aug 25, 2006
Posts: 9
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

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain the code