• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

java param access other param

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


and other method that takes this enum as param


in this method, I'd like to do type checking based on myEnum passed as the first param. the second param must be of same type as myEnum.fieldType.
How do I get this to work?
thanks
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Spades wrote:



Does that even compile?
 
Sheriff
Posts: 28408
101
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Spades wrote:in this method, I'd like to do type checking based on myEnum passed as the first param. the second param must be of same type as myEnum.fieldType.
How do I get this to work?



If you want the compiler to check the type of the second parameter, then the type of myEnum.fieldType must be known at compile time. Your posted code is a bit off (as per what Darryl said) and your example with only one enum member isn't sufficient, I don't think, but I doubt you're going to get it to work. But if you'd like to work on your example a bit, perhaps something can be done.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


sorry, typo in the code, it should've been like this.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can receive your second parameter as Object obj and test obj.getClass() == myEnum.getFieldType(). Or if it needn't be the exact same class, you can leverage Class#isAssignableFrom(...) or isInstance(...).
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, I already know about that, but that would be runtime check, no compile time check. I forgot to mention that I want to achieve compile-time checking.

so something like


thanks
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not possible, because the type is only known at runtime. During compile time the compiler isn't smart enough to see whether an A, B or C is required.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic