Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Can Logger be protected?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally we do this to instantiate logger:



Is there any concern if I do this:



 
Marshal
Posts: 25815
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I wouldn't normally write code in which the constructor of a class sets a static variable like that. But apart from that, if the code does what you want it to do then there's nothing wrong with it.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't implement like that. It will probably work if you only have one class extending your abstract class B but as soon as you introduce another class you will get confusing results.

The logger will always be linked to the latest instance created. Try the following code. It is unlikely to give your desired results.

 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw you test method could throw a nasty NullPointerException. You've declared it static so you don't need an instance of the class but the logger attribute will only be created after you have created an instance of a B.
 
Sheriff
Posts: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this:
The logger gets created once the class is loaded, and can never change. It also takes away the confusion that Paul's class described - the logger is always using class B for logging.
 
Abu Nene
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Beckett wrote:btw you test method could throw a nasty NullPointerException. You've declared it static so you don't need an instance of the class but the logger attribute will only be created after you have created an instance of a B.



Hi Paul, your points are valid. Guess I'll stick to instantiate the logger using the private modifier. Thanks!
 
Abu Nene
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:How about this:
The logger gets created once the class is loaded, and can never change. It also takes away the confusion that Paul's class described - the logger is always using class B for logging.



Hi Rob, what about class C? By doing that you are only instantiating the logger with class B as the parameter.
 
Rob Spoor
Sheriff
Posts: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class C will use the logger from class B.

If you want each class to have its own logger, then you have two options:
1) make the logger an instance field, not a static field. Each instance has its own logger. You will need instances to log though
2) give each class its own static logger; don't use B's logger in class C at all

As said before by Paul, if you share the logger field among all classes, then C will change B's logger, so B will start logging for class C. That's definitely not what you want.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic