Java Question

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
See if this.<variable> resolves to it (instance) or if <classname>.<variable> resolves to it (static) or if neither (local)

That ought to tell you.

My IDE (eclipse) tells me where a variable is defined.

Why do you need this?
 

irishScott

Lifer
Oct 10, 2006
21,562
3
0
Originally posted by: Thyme
See if this.<variable> resolves to it (instance) or if <classname>.<variable> resolves to it (static) or if neither (local)

That ought to tell you.

My IDE (eclipse) tells me where a variable is defined.

Why do you need this?

I'm a software engineering intern, and my boss wants me to write PMD (http://pmd.sourceforge.net) rules for code formatting. One of the rules is:

The class name shall always be used when referring to class (static) variables or methods. Object names shall not be used even though the Java language allows them. Unqualified references to class (static) variables shall not be used. Examples:
SomeClass.mStaticVariable; // good
SomeClass.staticMethod(); // good

SomeClass myObj;
myObj.mStaticVariable; // bad
myObj.staticMethod(); // bad

and the rule has to be written in either Java or XPath. I hate XPath (and can't find a way to accomplish it in XPath anyway). So it's Java or bust :p

Also, I told him about "Checkstyle" another sourceforge project made specifically for this kind of thing, but he hasn't gotton back to me on that yet.
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
Ah, cool. Either Eclipse or the 1.5 JDK will generate a warning when you access a static argument in a non-static way.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
if you ever need to do this dynamically it's easy using reflection...

Field f = Test.class.getField("tstring");
if (Modifier.isStatic(f.getModifiers()))
System.out.println("It is static");