Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a tough nut to crack

 
gudiya
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am having many class files in my java project,in which all the methods within the classes have some System.out.println(SOPs)in them. in some cases i need all the SOPs to be printed and in some cases i dont need any of these SOPs to be printed(situation similar to a switch being switched ON/OFF). how can i achieve this?

i can put all SOPs inside if loop havng some condtn but havng IFs for each SOP creates overhead... need a optimised solution....

can i go for overriding SOP?? is tat possible??
[ April 21, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"gudiya",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sahithya harsha:
can i go for overriding SOP??


That would be a very poor approach.

Rather, what you are describing is exactly the purpose of logging. You can use the built-in logging classes in java.util.logging, or popular 3rd-party packages such as Log4J.
[ April 21, 2007: Message edited by: Bear Bibeault ]
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can even create your own Logging class, where you can add a flag, which is configurable using a properties file, as to whether or not you wish to print the statements. This is useful because if your application is a small one, using Log4j is not advisable.

Njoy!!
Sid
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is useful because if your application is a small one, using Log4j is not advisable.

I can't understand why. I find it useful in any application. Can you please explain ?
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't spend time on debating the advantages or disadvantages of logging.
one might also advocate the use of AOP for this purpose- but that is beyond the point.

Gudiya,
the answer is simple.
Have a class like this

class Flags{

public static final boolean LOGGING=true;

}

and use it in your code

...
if(Flags.LOGGING){
System.out.println(...);
}
...

You're worried about the performance right? don't be. The moment you compile such a code, a good compiler will remove all the if(Flags.LOGGING) checks.
And if you set it to false, and recompile your code, it will remove all the code inside such if blocks including the checks.

but mind you, that's the only hitch- you need to recompile your entire code that uses this class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic