Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I call a method on a class without instantiating it?

For example, I call a method from Integer, like toHexString().

Now I need to print it out by the following code.

    int i = 123455;
    System.out.println(Integer.toHexString(i));

I haven't created a new object named Integer. Why I can use Integer, this object, directly?

like image 424
Zishuo Yang Avatar asked Dec 08 '22 23:12

Zishuo Yang


1 Answers

You can use it like that, because toHexString is a static method.

For static members, you don't need an instance, you can call them directly through the class.

like image 110
Stultuske Avatar answered Feb 22 '23 23:02

Stultuske