1

I want to check if a provided argument is a reference to a specific static field of a class, but want the method to be able to be passed the static reference directly and derive the Field property of the argument internally:

/*
 * I want:
 *
 * isCharacterCurrencySymbolReference(Character.CURRENCY_SYMBOL) to evaluate to true
 *
 * while something like:
 *
 * isCharacterCurrencySymbolReference(Character.LINE_SEPARATOR) to evaluate to false
 *
 */
public static boolean isCharacterCurrencySymbolReference(Byte staticCharacterField)
{
    return Character.class.getField("CURRENCY_SYMBOL") == staticCharacterField.getFieldThisArgumentIsReferenceTo();
}

Would something like that be possible or because the static reference is evaluated to a primitive byte at runtime make it impossible without the method just being passed a Field as an argument directly?

Snap
  • 70
  • 1
  • 10
  • 1
    Java is [call-by-value](https://stackoverflow.com/q/40480/2711488), so an argument can never be “a reference to a specific static field”. The argument value may be equal to the value of a field, but this is no proof that the caller used the particular field. – Holger May 04 '20 at 15:20
  • I had a feeling that this was the case but was hoping maybe I was wrong or there was a work around. I'm going to close out the question as answered stating the points you brought up. – Snap May 05 '20 at 05:48
  • 1
    It only works for dedicated types, where the value bears the same semantics as the field, i.e. `enum` constants and types following the same pattern. – Holger May 05 '20 at 06:30

2 Answers2

1

Make it a Byte instead of a byte. Since an instance of a Byte is an object instead of a primitive, it should work.

D.L.
  • 314
  • 1
  • 7
  • Except the issue is inside the method, unless I'm mistaken, that value will still just simply be a `Byte` without any way of checking whether it's derived via a reference to the `Character` static field, but I'll still edit my question since I think it slightly helps give it context by having it be an Object in the question. – Snap May 03 '20 at 08:47
  • Should work if the field is immutable... But if it's mutable, then you're right. – D.L. May 03 '20 at 08:51
  • Sounds like what you're looking for is something equivalent to the & operator in "C", and my understanding is that Java was specifically designed to avoid that sort of thing, although I can't guarantee there isn't some obscure reflection based method out there. – D.L. May 03 '20 at 08:54
  • My concern is that it's the way static members are evaluated. During runtime static values are simply their values so, as far as Java's concerned, the method receives a `byte`. It doesn't know where it came from. Hmmmm, but you did remind me Java does support some bitwise operations so maybe I can make that work somehow. – Snap May 03 '20 at 08:58
  • Essentially, I want this method to be a `Field` comparison/equality check, but be able to be passed a `Field`'s value as an argument but then still perform the comparison equality check on the passed argument's `Field` and not its actual value which I don't think is possible. At least the way I was hoping to do it. – Snap May 03 '20 at 09:02
  • Even if it's static a reference to a Byte is still an object reference so you should be able to compare it to another Byte instance (static or non-static) to see if they are the same object instance. But again, that really only works if you set the static reference once, or if you set it to the value of an enum or something like that. – D.L. May 03 '20 at 09:05
  • Actually, if you could implement this using Enums, that might be the way to go. – D.L. May 03 '20 at 09:09
  • But I'm not trying to see if it's a reference to a ’Byte’ field, I'm trying to see if it's a reference to a static ’Character’ field that happens to contain a ’byte’ value. – Snap May 03 '20 at 09:11
  • Except I want to be able to specifically pass the method something along the lines of Character.CURRENCY_SYMBOL or a different Character static field and then be able to confirm that the passed argument is in fact from the Character class (and not just a random byte) – Snap May 03 '20 at 09:15
  • If you want to check the type you could use the instanceof operator, but that's also going to require you to use Bytes and Characters instead of bytes and chars. I'm not sure you're going to be able to get a reference to a primitive in Java. – D.L. May 03 '20 at 09:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213004/discussion-between-d-l-and-snap). – D.L. May 03 '20 at 09:28
0

Since arguments are pass-by-value in Java, a method only has the value of the argument it's passed and therefore cannot be used as a reference to where it originated from.

Snap
  • 70
  • 1
  • 10