• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects in method arguments

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does doBarStuff(Foo f) mean? For e.g:-


In the Foo class, there are many methods and instance variables. When we write doBarStuff(Foo f), what does this f actually represent?

Do we need to do this --- f = new Foo() and then use this f to call the Foo methods & variables?



Why do we need to use an "Object as an argument" in another method??

Thanks.
edit: added curly bracers to code
[ August 28, 2008: Message edited by: LS chin ]
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it. A quick look at your previous questions showed that you have definitely used methods that take arguments before. What has changed recently that you don't understand how arguments work? Or are you asking about something specific?

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In response to your first question...

The items inside the parentheses are the arguments (parameters) passed to the method. Basically, these are things the method needs in order to do whatever it does. Some methods require arguments, and some do not.

If there are arguments inside the parentheses, you might think of this as "using." For example, doBarStuff(Foo f) means "doBarStuff using this Foo object called f."

In response to your second question...

An instance method (meaning a method that is not static) must be called using an instance of the class. One way to do this is the way you illustrated -- assigning an instance to a variable and then using that variable to call the method. (Another way is by using the keyword "this" -- either explicitly or implied -- in the correct context.)
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
I don't get it. A quick look at your previous questions showed that you have definitely used methods that take arguments before. What has changed recently that you don't understand how arguments work?

Okay I have to admit, this is kinda embarrassing. I tend to forget all these new-to-me Java syntax sometimes! All of a sudden I have absolutely no idea why a method can take arrays as arguments --e.g. main(String[] args). Yikes!! Hopefully this will not happen again. LoL.

Originally posted by Henry Wong:
Or are you asking about something specific?

No, it's nothing specific. I was reading 'inner classes' last night and suddenly, all syntax seems to look a bit weird... I guess I was confused. heheh

Thanks for your reply.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
The items inside the parentheses are the arguments (parameters) passed to the method. Basically, these are things the method needs in order to do whatever it does. Some methods require arguments, and some do not.

If there are arguments inside the parentheses, you might think of this as "using." For example, doBarStuff(Foo f) means "doBarStuff using this Foo object called f."

Thanks, Marc. Yes, you are right - this is what arguments are supposed to do. I've gotten a bout of programmer-mental-block! hahah


Originally posted by marc weber:
An instance method (meaning a method that is not static) must be called using an instance of the class. One way to do this is the way you illustrated -- assigning an instance to a variable and then using that variable to call the method. (Another way is by using the keyword "this" -- either explicitly or implied -- in the correct context.)


Thank you! If I forget this again, I need to jolt my memory with a knock the head. //rolleyes
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic