Forums Register Login

Access to static vs. local variables named the same

+Pie Number of slices to send: Send
Consider the following code:


This seems a bit awkward to me in that you need to create an instance of a class just to access the static member variable just because it is named the same as a local variable (not saying you have to, just the way it appears from this example). You don't have to create an instance of the Math class to use the various member variables (i.e. Math.PI) so I must be missing something. Can someone describe a better way to do this?
+Pie Number of slices to send: Send
Hi Chris,

Before I answer, note that this scenario seems extremely unlikely to ever come up in practice; no one who worked on any project I ran would ever be allowed to name a class variable "j"! If you use descriptive names for all variables, the chance that this would ever comes up falls roughly to zero.

But in any case, you don't need to create an object: the Math.PI syntax works fine here, too.

+Pie Number of slices to send: Send
Ernest,
The intent of the question was not meant to simulate a real world example. I contrived the example to make sure I understood the difference between accessing local variables, static variables, etc while studing for the Programmer exam. In any event, I agree, it is extremely unlikely to encounter in the real world but at least I will be prepared for the exam.
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 922 times.
Similar Threads
language fundementals
why I need to define a parameter variable as final?
compilation fails why?
use of super,this in static method
class types
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:48:14.