I have seen the following lines of code within various Java classes, and I am wondering what is the benefit of doing it each way, other than the two subjective arguments for each respective example of “its more obvious what is being referred to” and “its shorter”?
1:
public class MyClass extends OtherClass
{
private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
...
}
or 2:
public class MyClass extends OtherClass
{
private static final Logger LOGGER = LoggerFactory.getLogger(this.class);
...
}
I have seen both functioning seemingly exactly the same at runtime in logger outputs, however “this.class” is shorter and seems easier to copy-paste around when you are developing a large codebase, if only to cut back on copy-paste errors that may result from doing it the other way.
Is that the only consideration I should make when deciding which is preferred or are there other technical reasons an explicit name might be preferred over just using “this”? For example, does it maybe save a cycle or two at runtime to not have to lookup “this”‘s class on init?
Following the response to a similar question here, I concluded that it didn’t exactly match this situation, since its not a method on the object, but essentially a class definition I have the question about, so I am unsure if the accepted answer of “they become the same once compiled” holds in this situation.