File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Methods" Watch "Methods" New topic


Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I'm probably getting confused again, however I am just having a thought..

Is it possible to pass something into a method without actually calling a method?

For example.
Say in my MainProgram in another file I call method x. Which is a method in another file. Method x does lots of things like calculations and displaying various things to screen.

If in my MainProgram, in a separate file, I would like to pass something to method x
x(number1, number2)
BUT I do NOT wish x to display various things to screen

Is there something I can use to do this?
OR do I have to write another method or alternatively amend method x so it doesn't print things to screen?
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
Originally posted by Maureen Charlton:
Is it possible to pass something into a method without actually calling a method?
Sounds like a contradiction to me

You can call a method with parameters and have the method not use the parameters for anything:
Hope that helps.

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Otherway round.

I wish a method to do something with the parameters but exclude the println's for this particlar task. In other tasks I wish to use both the calculations and the println's.

Do I have to amend the method accordingly or can I do this with the same method?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You have complete control of what your methods do with their input parameters. You also have control over wether your methods return something, Sting for example or nothing i.e void
There is no fixed rule that a method must include code that writes to output.
Please post your code and explain what you are trying to do and we will try and help
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Is it possible to add a boolean parameter to your function?

if so, you'd have

note that you could still have a version that didn't have the boolean:

this way, you could leave all the calls that you already have that you DO want to print alone, and just change the ones where you don't want to print to HAVE the third parameter with FALSE as the value for param 3

did that make ANY sense???
[ November 24, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dave Cryder

Joined: Aug 11, 2004
Posts: 9

There's any number of ways you could do this. This is just one example

The object of class SomeClass would be created with printing disabled. You could call setSomeField() as often as you liked, changing the value of the field each time. When you call setPrintingStatus() with the parameter set to "true," the program will output the field to the system's output stream.

Keeping track of the object's internal state is what fields are for. Generally, it is a good idea to use methods to change field values (rather than making the fields themselves directly available by making them public).

Hope this helps!
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218

Haven't got any code I was just thinking of a way that I could do it rather than writing two separate methods.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

I would rather write two methods. If you would like to improve as a programmer, one important principle you should learn is the One Responsibility Rule. If a method needs to do calculations, that's all it should do. If it needs to display things, it should only display things. Ne'er the twain shall meet. To put conditionals in the method to control whether it will display things or not is kludgy at best, IMO.

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: Methods
jQuery in Action, 3rd edition