Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain the code

 
anamika henry
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 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
Java Mac Safari
  • 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.
 
Patrick Patel
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
Java Mac Safari
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic