This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes a tough nut to crack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a tough nut to crack" Watch "a tough nut to crack" New topic

a tough nut to crack


Joined: Apr 21, 2007
Posts: 1
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

Joined: Jan 10, 2002
Posts: 63208


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.

JavaRanch Sheriff

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63208

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

Joined: Feb 20, 2007
Posts: 152
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.

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

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 ?

[My Blog]
All roads lead to JavaRanch
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
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.

the answer is simple.
Have a class like this

class Flags{

public static final boolean LOGGING=true;


and use it in your code


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.

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: a tough nut to crack
It's not a secret anymore!