java


How to check if an argument is pointing to the right field in Java


I need to write some method to accomplish one thing and I've no idea how to do it. I'd appreciate any help.
Suppose we have a method
void m1(int z) {}
which is called like this:
int x; ... m1(x);
Basically I need to make sure that method m1 is using exactly the 'x' field that I need. Not the value of the 'x' field but that it's pointing to the field 'x' and not some other field of this or any other class.
To be more specific I was going to write an if-statement meaning:
if (z is pointing to the field x) return true;
and I don't know how to realize that pointing condition. Is there a standard method for such evaluation?
Thank you very much for your replies, all of you guys.
#durbnpoisn, #Boris the Spider
The code per se doesn't exist yet but I'm trying to elaborate the approaches. I'm not an experienced programmer that's why it may look weird. Sorry for that.
But if it helps my task is to make several 'switch' buttons that when are pushed toggle off certain fields and methods from different parts of the code as if they never been used.
Like for example, the code line looks like that supCrossVal = x^realOpt/setPower(mine) with all switches off and it should execute like it's supCrossVal = x^realOpt when SWITCH1 is on.
The problem is that the initial code is quite big (so as the number of related calls) and the number of switches would be around 10-15 and some of the swithes can turn off the same methods and fields. So it seemed to me that adding if-statements (checking whether switchs are on or off) to every field or method call will make the code too hard to read. And overloading methods for each switch seemed too straightforward and code littering as well. So I was thinking of a simple method based on that idea of pointing.
Primitives are value types and so there is no notion of "pointing" to them. If you want to ensure that two variables are pointing to the same object, they have to be reference types. In your case you can achieve this by using the boxed type Integer instead of int (for your method parameter and the field), and using == for comparison. ==, when used with reference types, compares the reference themselves, and so will return true if both are pointing to the same object. In contrast, == with primitive types returns true if the values are the same.
As Boris mentioned, this won't work if the the values are in the range [-128, 127] since those are cached, and so in Integer x = 127; Integer y = 127;, x and y refer to the same instance.

Related Links

Java Abstract class to instationize other class
Calling java method from unity - field and class name different in error log
How do I make an IF statement that compares 2 integers assigned to 2 string variables in 1 class then execute the code in another class in Java?
Spring Boot - Bean creation exception
Java File Monitor
Applying UDT on Spring Data
Issue uploading external apps in IBM MobileFirst Application Center Console with proxy
Why am I getting “<identifier>Expected” when I am just trying to use System.out.println(“name”)? [closed]
Can I split a glass pane up into two, and add a seperate mouselistener to each?
How do I access information about the JSP from within a custom tag (TagSupport)
JavaFX: Opens a blank stage which stops responding
BlueJ Java MySQL CRUD application | Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
error cannot find symbol variable id
Why JAR Files Do Not Contain Documentation?
Android WebServices PHP MYSQL
Android Studio: Virtual Device

Categories

HOME
ionic-framework
reflection
jsp-tags
leon
ojdbc
acquia
multiple-records
contact
slurm
google-translate
vifm
mapserver
alignment
visual-studio-cordova
flask-wtforms
netflix
lombok
django-simple-history
pc
oxyplot
cx-freeze
sylius
extjs5
hammerspoon
crosstab
crystal-reports-2010
spark-jobserver
dynamic-featured-image
chromebook
facebook-apps
libssl
delicious-api
google-api-nodejs-client
mapdb
unspecified
hilbert-curve
ncalc
sql-server-agent
glew
eigenvalue
estimote
modelmapper
segment
cubic-spline
auto-update
avconv
flickr-api
errordocument
multipeer-connectivity
celery-task
snmptrapd
qtwebview
iso8601
spring-mongodb
dwscript
django-scheduler
minimization
crosswalk-runtime
angular-strap
android-textview
py2app
bstr
storekit
pyke
phpcas
freedesktop.org
xna-4.0
msys
google-places
spidermonkey
pundit
tarjans-algorithm
embedded-code
ideamart
ami
web-controls
c++03
oxwall
knuth
undefined-reference
rtmfp
concurrent-collections
commoncrypto
sitemesh
easy-install
type-equivalence
smtp-auth
tinn-r
newtonscript
reddot
bigcouch
qt-jambi
mod-auth
imac
rescale
swing-app-framework
simpletest
noscript
nerddinner
ntvdm.exe

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App