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

passing args to a class

Dale Smirth
Greenhorn

Joined: May 17, 2005
Posts: 14
I have a class that generates four variables. These four variables are generated by a Timer thread.

What approach should be taken so that I can pass those four variables as arguments to a class as parameters?

I am not sure whether I can pass these variables directly to a class.

Basically, my timer thread comes up with 4 vars and those 4 vars will be evaluated my the class that I need to accept the 4 vars.

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Basically, the class receiving these 4 vars should probably have a constructor or method taking these 4 vars as arguments...


"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
Dale Smirth
Greenhorn

Joined: May 17, 2005
Posts: 14
Thanks,

Are there any pros or cons to either using a constructor or method?

I am familiar with using constructors, but I am still wondering about whether a method would be better. Well - now that I think about it, it seems that a constructor would be needed regardless of whether I want to use a method or not...

After all, the class still has to 'SEE' the parameters that are passed and the class method could only 'see' what class that it's a part of - right?

Also, these parameters will be constantly passed to my class object over time (triggered by my Timer thread.)

So, now that I think about it - I may want to go the method route, since I will not be creating new objects every time I pass arguments.

Hmpf - still thinking about it...
[ August 09, 2005: Message edited by: Dale Smirth ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Look at the Factory Method design pattern on the net. AND/or get these books: Design Pattern AND / or Effective Java.


[ August 09, 2005: Message edited by: Jean-Sebastien Abella ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dale Smirth:
... Are there any pros or cons to either using a constructor or method? ...

Consider that a constructor's job is basically getting an instance of that class ready for use (constructing an object). It really depends on what you have in mind for this class, but if the purpose is just to "evaluate" variables, then it seems that a method might be more appropriate, so that you're not creating a new object every time you want to evaluate a new set of varibles. In fact, a static method might be best, because then you wouldn't even need an instance. (As Jean-Sebastien Abella pointed out above, see "factory method.")
Dale Smirth
Greenhorn

Joined: May 17, 2005
Posts: 14
Thanks marc - i will attempt implement these ideas in my code.

Peace


As for a static method - I am only used to use the main() method as a static method. I kinda follow where you are going, but I am not totally sure.

If I run into problems, I will just add to this thread.

Later
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If a method is non-static (in other words, an "instance method"), then an instance of the class must be created for that method to be called.

But if a method is static, then it is associated with the class rather than an instance of that class. No object of that class needs to be created.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing args to a class