wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Objects in method arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects in method arguments" Watch "Objects in method arguments" New topic
Author

Objects in method arguments

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"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
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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

Joined: Jun 28, 2008
Posts: 99
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects in method arguments
 
Similar Threads
Trying to access the instance variable of Class Bar from Class Foo --------- why error ?
C++'s out parameter object creation and JNA
Anonymous Inner Class
creating objects
An Error In Program.