This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing args to a class

 
Dale Smirth
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, the class receiving these 4 vars should probably have a constructor or method taking these 4 vars as arguments...
 
Dale Smirth
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic