Reputation: 81
As per my knowledge, static members can be accessed directly. But if someone wants to access it by creating object of it, is this possible in Java?
Upvotes: 0
Views: 82
Reputation: 3914
As the others said, it is possible.
But you shouldn't do it, because it is error prone. Using the class for static access, it is easy to recognize what you meant to do, but if you use the object, one could think, that you want to access a field.
Upvotes: 2
Reputation: 30216
Well surely. You can even do the following:
Foo foo = null;
foo.staticMethod();
But it's kind of pointless and will result in warnings.
Upvotes: 1
Reputation: 46127
Yes, it is possible, but such a need should ideally NEVER arrive. If it does, check your object design again.
Static methods, by definition, should be accessed via the class reference.
Take a read through this - http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Upvotes: 1
Reputation: 16035
To put it shortly, yes, it's possible, but I don't see any reason to do it (if all you want is to access the static member).
Upvotes: 0
Reputation: 6623
Yes it is possible but you'll get a warning for it, and there is absolutely no reason to do so.
Upvotes: 0