File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

a tough nut to crack

gudiya
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

"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


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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.

Njoy!!
Sid
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.

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.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a tough nut to crack