aspose file tools*
The moose likes Beginning Java and the fly likes Method parameters question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method parameters question" Watch "Method parameters question" New topic
Author

Method parameters question

Joseph Potts
Greenhorn

Joined: Jul 21, 2012
Posts: 7
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

Joined: Oct 24, 2010
Posts: 563
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Joseph Potts
Greenhorn

Joined: Jul 21, 2012
Posts: 7
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

Joined: Oct 24, 2010
Posts: 563
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

Joined: Jul 21, 2012
Posts: 7
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

Joined: Oct 24, 2010
Posts: 563
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method parameters question