Take the 2-minute tour ×

Can anyone explain to me why java allows you to access static methods and members from an instance? A bad example, if I have an object called RedShape and it has a static method called getColor() which returns "red", why does java allow you to call the static method from an instance of RedShape? To me this seems to violate some of the core concepts of OO language design. At the least, it should come with a compiler warning.

Thanks in advance.


In particular, I'm asking about when you have something like

RedShape test = new RedShape();

where getColor is a static method of the RedShape class. This doesn't make any sense that it's allowed and doesn't give a compiler warning on the command line via javac. I see that it's "strongly discouraged", but was curious if there was a technical or reasonable reason behind why it's allowed outside of "because C++ allows it."

share|improve this question

5 Answers 5

up vote 2 down vote accepted

There's really no reason why you can actually do this.

My only guess was that it would allow you to override static methods, but you can't.

If you try the following scenario:

Banana has a static method called 'test' (this prints 'banana') Apple extends Banana and "overrides" the static method called 'test' (this prints 'apple')

and you do something like this:

public static void main(String[] args) {
    Apple apple = new Apple();
    Banana banana = new Banana();
    Banana base = new Apple();


The resulting output is:


So effectively, it's pretty useless.

share|improve this answer

I don't see anything wrong with calling a static method from an instance. What's wrong with that? In particular, quite often there are methods which are useful within the logic of a class, but which don't actually need to manipulate the instance itself.

I do object to calling a static method via an instance reference. Classic example:

Thread thread = new Thread(...);
thread.sleep(5000); // Doesn't do what it looks like

This comes with a compiler warning in some IDEs - certainly in Eclipse, assuming you turn it on. (Java / Compiler / Errors and Warnings / Code Style / Non-static access to static member.) Personally I consider that a mistake in the design of Java. (It's one of the mistakes that C# managed to avoid copying.)

share|improve this answer
I think Brian is referring strictly to the latter abuse. –  erickson Sep 17 '09 at 16:23
Luckily I believe sleep is usually called on the current thread. google.com/codesearch?as_q=currentThread();\.sleep&btnG=Search+Code&‌​hl=en&as_lang=&as_license_restrict=i&as_license=&as_package=&as_filename=&as_case‌​= –  Tom Hawtin - tackline Sep 17 '09 at 16:43
Er, tinyurl.com/laojcp –  Tom Hawtin - tackline Sep 17 '09 at 16:43
@Tom Hawtin: Exactly. The code doesn't do what it looks like it will do. –  Jon Skeet Sep 17 '09 at 16:53
@erickson: You are correct. I was referring to the ability to reference a static method from an instance of the class containing the static method. I've revised my question to clarify. –  Brian Hasden Sep 17 '09 at 17:05

The access to static methods allows you to share values between instances of the same class or even get the values without needed to create a class instance.

There are cases where it's convenient and is no OO language violation.

share|improve this answer

I bet it's because the original designers were porting capability from C++, and by the time 20/20 hindsight hit, it was a backwards compatibility issue.

That, or because when you call a method within a class, even though you don't have to prefix everything with this. the compiler inserts it (or equivalent) including for static methods. If static methods couldn't be called from instances, then tacking this. on the front might be a problem (or would force coders to tack class name on the front of static methods whenever they wanted to use them within an actual instance).

Anyway, the answers are speculative unless we get one of the early language developers to answer.

share|improve this answer
public class MyClass {
    public static String myString;

public class AnotherClass {
   public void doSomething() {
   public static doAnotherThing() {
       MyClass.myString = "something";

Here we are accessing static variable from non-static method (indirectly) by calling static method from non static method.

share|improve this answer

Your Answer


