• 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

Using a class as a parameter in a method

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I am using a POJO ( a very simple class: some variables + getters and setters) to store info of bikes. For example:



Therefore, each bike is represented by one POJO.

I would like to know if it is possible to create a method which takes the class as parameter. For example:



So I can just pass one parameter (the class) instead of having to execute all the getters in that class to obtain all the variables and then call the method with thousand parameters.

Thanks in advance

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
probably, reflection mechanism fit for your requirement
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your requirement is unclear. Can't you just pass the instance of the object?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear what you mean exactly.

Mnau Lubi wrote:So I can just pass one parameter (the class) instead of having to execute all the getters in that class to obtain all the variables and then call the method with thousand parameters.


Do you mean that you want to set the fields of all existing instances of a certain class to certain values? There is no way in Java to find all existing instances of a class, if you only know the class. Java doesn't keep track of all the instances of a class; you have to do that yourself. So if that's what you mean, then that's not possible.

If you have all your Track objects in a collection, for example a List, you could ofcourse pass that List to your method:


Or... are you maybe confusing the terms "class" and "object", and do you simply mean that you want to pass a Track object to your method:
 
Mnau Lubi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, sorry I was confusing class with object
 
reply
    Bookmark Topic Watch Topic
  • New Topic