• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method parameters question

 
Joseph Potts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading Head First Java, and let me take a minute and compliment that it's a VERY good book, I'd recommend it to anyone who is starting with the Java Programming language. I'm currently on Chapter 4.

I wanted to ask what's the major difference between these two codes:


and



Are method parameters a part of conventions or is there a special use for them? Because I've been interpreting their use and I don't see the difference if I was just to put the parameter value inside of the method.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are method parameters a part of conventions or is there a special use for them?


I think you should keep reading and do some example code that uses methods, and I believe the difference between the two methods will become clear - er.

The most significant difference is the source of the value of the variable numOfBarks. In the first example, the value is passed to the method as a parameter named numOfBarks which is then a local variable to that method. In the second, the variable numOfBarks exists outside the method and is set by another part of the program outside the view of what you've posted. There are valid uses for both approaches.
 
Joseph Potts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:
Are method parameters a part of conventions or is there a special use for them?


I think you should keep reading and do some example code that uses methods, and I believe the difference between the two methods will become clear - er.

The most significant difference is the source of the value of the variable numOfBarks. In the first example, the value is passed to the method as a parameter named numOfBarks which is then a local variable to that method. In the second, the variable numOfBarks exists outside the method and is set by another part of the program outside the view of what you've posted. There are valid uses for both approaches.


It's posted inside the method, or at least it's a part of the method.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's posted inside the method, or at least it's a part of the method.

"It" meaning the variable numOfBarks? You're absolutely right in that the variable numOfBarks exists inside both methods, and is declared in the method signature in the first and in the method body in the second. (I missed the 'int' in the second method on my first read.)

So, how is the value of the variable numOfBarks defined in the second method? What is the value of numOfBarks in the second method? Since it's a local variable, local to the method, will the source code even compile as it's currently written?
 
Joseph Potts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:
It's posted inside the method, or at least it's a part of the method.

"It" meaning the variable numOfBarks? You're absolutely right in that the variable numOfBarks exists inside both methods, and is declared in the method signature in the first and in the method body in the second. (I missed the 'int' in the second method on my first read.)

So, how is the value of the variable numOfBarks defined in the second method? What is the value of numOfBarks in the second method? Since it's a local variable, local to the method, will the source code even compile as it's currently written?


Obviously it won't compile, it's a chunk of code I took out of Head First Java (the first example at least). I just wanted to know the difference between the two ways.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must be having an off day. I'll retire to my own projects and confine my tendency to confuse to myself.

Good luck.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second example would compile if you did this:


But now, every time you call "bark()", you will get exactly three "ruff"s. In the first example, you could call "bark(7)" or "bark(0)" or "bark(2873)", and it would print "ruff" that many times.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic