This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access to static vs. local variables named the same

 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic