• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing args to a class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic