Private Logger Log=logger.getlogger (This.getclass ()) in Java;

Source: Internet
Author: User

What does This.getclass () get?

This represents a reference to the current object;

GetClass () is a method in Java.lang.Object that returns the run-time class of an object;
This.getclass () is the run-time class that returns the current object.

What does Logger.getlogger (This.getclass ()) get?
He gets a logger object, and this logger will monitor the This.getclass () This runtime class, within which you may create log.info (""), Log.debug (""), ... Statements, these statements will output your logs based on your pre-defined logger level. As you write System.out.print (""), the difference is that logger can be controlled according to the level of log output as needed. (This is, of course, just one aspect)

Logger.getlogger (This.getclass ()) What are the benefits of writing this?
So you just have to write a line of code in the base class, and the subclass can be used directly, which is the principle of reuse.

  1. Package com.zhaipuhong.j2se.keywords;
  2. Public class Thiskeywordsa {
  3. protected String className = this.getclass (). toString ();
  4. Public Thiskeywordsa () {
  5. System.out.println ("thiskeywordsa className = =" + ClassName);
  6. }
  7. }
  8. Package com.zhaipuhong.j2se.keywords;
  9. Public class THISKEYWORDSB extends thiskeywordsa{
  10. Public THISKEYWORDSB () {
  11. System.out.println ("THISKEYWORDSB className = =" + ClassName);
  12. }
  13. /** 
  14. * @param args
  15. */
  16. public static void Main (string[] args) {
  17. //TODO auto-generated method stub
  18. THISKEYWORDSB B = new THISKEYWORDSB ();
  19. }
  20. }


Operation Result:
THISKEYWORDSA ClassName = = Class Com.zhaipuhong.j2se.keywords.ThisKeywordsB
THISKEYWORDSB ClassName = = Class Com.zhaipuhong.j2se.keywords.ThisKeywordsB

Because B inherits A,a objects are first created (do not consider abstract classes and interface ^_^) and then create B objects as the word objects of the B object. The logger at this point is part of the B object and can be used by the B object.

This refers to the object of the child class

Private Logger Log=logger.getlogger (This.getclass ()) in Java;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.