Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Oftentimes people will shorten System.out to just out and make an analog called in (e.g. Scanner in = new Scanner(System.in);). I've seen two methods of doing this

Method 1:

import static java.lang.System.out;

Method 2:

static final PrintStream out = System.out;

Is there a coding convention or best practices for this? If so, which one is it and why is it more readable?

share|improve this question

closed as off topic by Jeff Vanzella, Brian Reichle, Bobson, Jerry Coffin, James Khoury Jan 30 '13 at 4:41

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Consider the scopes of the name out in each case. Method one is throughout the file and cannot be easily overridden (doing so will only add to the confusion). Method two restricts its use to the declaring class. What do you think is better? –  Jeff Mercado Jan 17 '13 at 5:46
    
People are voting to close. Does it not seem constructive? I figured it had an appropriate level of subjectiveness when adjusting for SE CodeReview standards. Is there a way I can reword it, so that it doesn't get closed? –  Eva Jan 17 '13 at 20:13
    
The reason they're voting to close is because this forum is dedicated to reviewing the code, e.g. you have a method that is working slowly, or you want to improve part of your code to correspond best practices, etc. This question just asks about people habits, and there is no actual code to review. –  almaz Jan 22 '13 at 11:25
    
@almaz I want to improve my code that uses System.out to correspond to best practices. I asked if it was a matter of personal preference because I wasn't sure if a best practice exists. –  Eva Jan 22 '13 at 21:32
add comment

2 Answers 2

up vote 6 down vote accepted

With the second approach you hide that out refers to System.out, so if anytime later you want to redirect the output, you need to change only static final PrintStream out = System.out; since the rest of the code is only aware of the fact that it is writing to a PrintStream.

The first approach is just using the shortcut offered by static imports.

Which one to use depends on the purpose of the code. If it is a one time, throwaway code trying out something, then I'd go with the first, otherwise the second, if there are many print statements. Otherwise I would not care replacing System.out.

On the other hand I would consider using a logger instead of System.out and that could be redirected to System.out to a File and to many other targets.

share|improve this answer
add comment

Is one more readable than the other or is it just a matter of personal preference?

Well, depends on your goals. I do not like both of them, so I would answer no to the first part.
Static imports will confuse readers without some IDE, because it is very hard to find out the origin of the statement. A member or class variable could be ok if you do not modify anything about System.*, but I do not see the point to save a System. (Anyway, you could have the same argument as for Method1.)
Not that heavy arguments, more a matter of personal preference. This is the reason i used the word like in the beginning. I prefer System.out(.prinln()).
One difference between this two methods is the effect of System.setOut(). Method1 is affected, Method2 does not care.

To emphasize it (it was already touched by Andras):

Method 3:

  • static void print(Object object) { some implementation, e.g. System.out.println(o) }

This is clearly shorter and more flexible. If you need more you could choose any of the available logging possibilities.

(Just for completeness: This flexibility could be reached with the other methods, too. Method1: Static import of some custom class with a public class variable out which has the necessary methods. Method2: Modify the variable type to a custom class with the same public class variable out which has the necessary methods)

share|improve this answer
    
I think the ACM library does it that way. –  Eva Jan 22 '13 at 21:35
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.