• 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

Java 'Getters' - How to re-utilize them?

 
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys! I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel.



And this is a snippet from the class which is using this class:


My question is, instead of creating a huge number of getters and then remembering which getter I need, is there a way to re-use the same one? Like maybe I can add a parameter? Thanks!
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Scabbia wrote:Hey guys! I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel.



Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement to return the panel associated with the enum value. From an object-oriented design perspective, though, the approach in code you already posted (where there's one accessor defined per panel) is preferable.
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brett Spell wrote:

Benjamin Scabbia wrote:Hey guys! I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel.



Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement to return the panel associated with the enum value. From an object-oriented design perspective, though, the approach in code you already posted (where there's one accessor defined per panel) is preferable.



Thanks Brett, after further research I think it will be the most effective way... although I could map my JPanels in a map with Strings as keys and they get them by name which provide the getter methods as arguments.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Scabbia wrote:although I could map my JPanels in a map with Strings as keys and they get them by name which provide the getter methods as arguments.


If you do go this route, then as Brett said, you should use an enumerated type instead of Strings as keys - it reduces the chance of errors due to invalid strings being used as the parameter
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with the individual getters?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brett Spell wrote: . . .
Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement . . .

And calling your class Calendar? Look at its get() method.

That was one of the reasons everybody complained about the pre‑Java8 date‑time classes.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Look at its get() method.


Exactly why I asked by question. I think the OP is being led into the reeds.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Brett Spell wrote: . . .
Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement . . .

And calling your class Calendar? Look at its get() method.

That was one of the reasons everybody complained about the pre‑Java8 date‑time classes.



Huh?
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Campbell Ritchie wrote:Look at its get() method.


Exactly why I asked by question. I think the OP is being led into the reeds.



Being led into which reeds and by who? Are we all talking about the same thread?
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that doing things like:

is an improvement over:

Also, of course, violating the bean conventions.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I don't think that doing things like . . . is an improvement . . .



Neither do I, which is why I (and at least one other person) recommended against it, so I'm not sure why you suggested that someone (me?) was "leading him into the weeds".
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps worded too strongly, but this is not a direction that the OP should go.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Perhaps worded too strongly, but this is not a direction that the OP should go.



Again, preaching to the choir, since nobody has suggested otherwise and in fact just the opposite.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic