java – class. Is getname () expensive?

In many courses of a project I'm working on, I see it called class_ The private static end member of name, which is defined as follows:

public class MyClass {
  private static final String CLASS_NAME = MyClass.class.getName();
  //...
}

What's the advantage (if any) of doing this to get the class name instead of MyClass. Where the name is needed class. Getname() call?

resolvent

Solution

This is class Implementation of getname()

public String getName() {
if (name == null)
    name = getName0();
return name;
}

As you can see, the value of the name is cached, so the call is not too expensive This is just the call of ordinary getters Anyway, if you call often, it may be a good style for defining constants

The above is a collection of Java - class Is getname () expensive? I hope this article can help you solve Java - class Is getname () expensive? Program development problems encountered.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>