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

Oracle 12C boolean Variable initialization issue
Method to count edges from root to furthest node in tree
Android Studio: Images do not show up while debugging app on phone, but do on the emulator
How to exclude a maven test scope from package phase?
java.lang.T : Can't find this type
Offline inclusion of XMLSchema with xjc
Output the lines that contain closing html tags
How to set the file polling frequency according to the Quartz cronExpression in mule
Java constructor error in child class
java log4j add one more logger from your code
Cron job infinite loop
No trusted certificate found for hdfs fsck / command
Combining StepExecution context late-binding and property placeholder in Spring
How setcontentview in a new activity depending on the button clicked
How methods of java.nio.Path works
Remove special characters from string for Http request data in java

Categories

HOME
compiler-construction
pandas
vim
c#-4.0
tinymce
dot
alfresco
read-eval-print-loop
routes
rsync
rdf
disassembler
angular-ui-bootstrap
ravendb
mvvmcross
acquia
qore
facebook-messenger-bot
amazon-cloudformation
maude-system
kentor-authservices
floating-action-button
postgres-xl
windows-azure-storage
n-gram
moonmail
commonmark
dtrace
iron-router
undefined
samoa
lucene.net
abi
wkwebview
google-search-api
cultureinfo
microsoft-sync-framework
status
kannel
jspm
libssl
noraui
winrt-xaml-toolkit
code-contracts
picasso
adobe-premiere
swisscomdev
hot-module-replacement
xenforo
awt
ws-security
fedex
nand2tetris
gpx
node-sass
convertapi
theming
boost-multi-index
sage-one
integrity
togetherjs
idisposable
recursive-datastructures
akka-cluster
pdfclown
quartz-composer
angular-cache
savon
django-debug-toolbar
gridview-sorting
whois
jqgrid-formatter
quicklisp
security-testing
dereference
dia
dd
file-copying
asp.net-web-api-odata
gulp-less
internet-connection
elliptic-curve
htmlcleaner
resty-gwt
oracle-warehouse-builder
stxxl
uv-mapping
geos
jmapviewer
dbconnection
easy-install
flash-builder4.5
buster.js
datawindow
newtonscript
mod-auth
horizontal-accordion
servicehost
html-input
sudzc
subviews
dmx512
dbal
vc90
photoshop-cs4
mtj
boost-smart-ptr
sustainable-pace
pascal-fc
dbisam
windows-live-messenger
3gp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App