Reputation: 2220
The wrapper class Integer
has the static method parseInt()
which is used like this:
Integer.parseInt();
I thought only methods of static classes could be called like this (i.e. Class.doMethod()
). All non-static classes need objects to be instantiated to use their methods.
I checked the API, and apparently Integer
is declared as public final Integer
- not static.
Upvotes: 0
Views: 2252
Reputation: 59607
Any class can contain both static
and non-static methods. When calling the static
methods on any class - including your own - you don't need to instantiate an instance of the class, just call the method using the class name: MyClass.methodName()
.
In fact, even the following will work:
Integer nullInt = null;
nullInt.parseInt("5");
This works because only the class type of the reference is important when calling static
methods. But consider this poor style: always use e.g. Integer.parseInt
instead.
Also note that you can't declare a top-level class as static
anyway: only nested/inner classes can be declared as static.
Upvotes: 7
Reputation: 106390
A way to phrase it: An Integer
is a concrete object; you can have many Integer
s. There is only one Integer.MAX_VALUE
.
That's to say, there are some things with Integer
s that are concrete, and others that only need to exist once, anywhere.
Upvotes: 0
Reputation: 686
In java, static methods may be called from objects, but this only generates a warning and still compiles. A non-static class can have static fields and methods that are shared by all instances (this is why "Shared" means static in VB.NET). Therefore accessing a static member from an object can confuse the reader, and must be avoided.
Upvotes: 2
Reputation: 323
No, you are wrong.
Only static methods can be called like this, but they may belong to 'non static' classes.
Upvotes: 2