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?

3
  • 1
    Java is call-by-value, 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. Commented May 4, 2020 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. Commented May 5, 2020 at 5: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. Commented May 5, 2020 at 6:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
Should work if the field is immutable... But if it's mutable, then you're right.
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.
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.
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.