英文:
Check if argument is a reference to a class' specific static field
问题
/*
* 我想要:
*
* isCharacterCurrencySymbolReference(Character.CURRENCY_SYMBOL) 评估为 true
*
* 而像这样的:
*
* isCharacterCurrencySymbolReference(Character.LINE_SEPARATOR) 评估为 false
*
*/
public static boolean isCharacterCurrencySymbolReference(Byte staticCharacterField)
{
return Character.class.getField("CURRENCY_SYMBOL") == staticCharacterField.getFieldThisArgumentIsReferenceTo();
}
英文:
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?
答案1
得分: 1
将它变成一个Byte而不是一个byte。由于Byte的实例是一个对象而不是一个原始类型,所以它应该可以工作。
英文:
Make it a Byte instead of a byte. Since an instance of a Byte is an object instead of a primitive, it should work.
答案2
得分: 0
因为在Java中,参数是按值传递的,所以方法只有所传参数的值,并且不能被用作指向其来源的引用。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论