calling static method in java [duplicate]

旧时模样 提交于 2019-12-03 06:52:25
PermGenError

In the Byte code

Test4 t4 = null;
t4.method();

will be

Test4 t4 = null;
Test4.method();

Compiler would convert the call with the class name for static methods. refer to this question on SO which i myself have asked it.

It doesn't matter if the instance is null, because you are calling a static method. Think of it this way.

Every static method is equivalent with a class method whereas a non-static method is equivalent with an instance method.

Therefor it doesn't matter what value the instance takes as long as you are working with static methods or members.

Static methods can be called via the classname or an instance. I would try to avoid to call them by an instance (also a lot of tools warn you to do so because of bad practice).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!