Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Accessing Static Members from Non-Static Context

807580Nov 9 2009 — edited Nov 16 2009
The question is: why?

Why does Java allow you to access static members from a non-static context, even if it does warn you? What is (or was) the justification?

I have heard a couple of attempts at justification. One went something like this: That way, you can, if you later desire, turn an instance member into a static member without adversely affecting code calling your function.

So I cooked up this example:
//Class1.java
public class Class1 {
   public /*static*/ void method() {
      System.out.println("Hello, world!");
   }
}

//Class2.java
public class Class2 {
   public static void main(String[] args) {
      Class1 c1 = new Class1();
      c1.method();
   }
}

//Shell:
$ javac Class1.java Class2.java
$ java Class2
Hello, world!
$ vim Class1.java #uncommented "static" keyword"
$ javac Class1.java
$ java Class2
Exception in thread "main" java.lang.IncompatibleClassChangeError: Expecting non-static method Class1.method()V
        at Class2.main(Class2.java:5)
$ javac Class2.java
$ java Class2
Hello, world!
{code}
In other words, you would +still+ have to recompile Class2 to allow the changes to Class1.  And if you recompile, why not give an error that simply requires the user to change to a static context.

The second use that I could think of was something like this:
{code}
   public static void main(String[] args) {
      /*Strict*/Math math = null;
      double d = math.PI;
      d = math.pow(d, 3.5);
      double e = math.asin(d);
   }{code}
In this case, I could change the entire method to use StrictMath instead of Math with one easy change.  But surely this wasn't the intended motivation, was it?  Clearly we should just use an interface and a factory method for something like this?

So there's my attempt to justify it.  But there are *so many things wrong* about accessing static members from a non-static context.  Here are a quick few:
1) It's not immediately evident that the member you are accessing is even static.
2) As a result, you can do things that, based on what you see, look like they should obviously throw an NPE (Eclipse even complains about the math.PI reference in the example above, because it can only be null at that point!).
3) It gives the appearance of a virtual method when it's really not.  It just opens the door to confusion.

So why do *you* think this was even allowed?  Is there a valid use?

Finally, a more practical question: does anybody know of a way to make javac give a compile *error* on these instances?  I'd at the very least like to prevent myself, or anybody I work with, from using static members this way.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 14 2009
Added on Nov 9 2009
14 comments
1,327 views