Hi folks,
I'm very new to java and learning via some old library programming books. I'm about 1/4 into these texts and have a very simple question. In the programming examples in the beginning, they output string using the "System.out.print()" . Then later they moved to using "StdOut.print()". Seems like its 2 ways of doing the same thing. Question is how do you know which to use and when? Ok, you may destroy me now...
Beginners quest in learning java
Started by joules32, Nov 27 2008 11:44 AM
5 replies to this topic
#1
Posted 27 November 2008 - 11:44 AM
|
|
|
#2
Posted 30 November 2008 - 11:24 AM
There is no StdOut class in the JRE. I suspect your tutorial binds System.out to a StdOut variable so that it can call print without typing out the System.out bit. This is poor practice and it should use a static import instead.
Anyway check for a line that looks something like
If that exists it means they have bound the System.out stream to a local variable. If that is the case then they are both equivalent and frankly it is unneeded. I'd use a static import.
Then you can call 'out.print()'.
Anyway check for a line that looks something like
PrintStream StdOut = System.out;
If that exists it means they have bound the System.out stream to a local variable. If that is the case then they are both equivalent and frankly it is unneeded. I'd use a static import.
import static System.out;
Then you can call 'out.print()'.
#3
Posted 30 November 2008 - 12:35 PM
Why is that a bad practice?
I didn't know you could do that.
Is is better to do the static import or just type System.out.println()?
I didn't know you could do that.
Is is better to do the static import or just type System.out.println()?
#4
Posted 30 November 2008 - 01:06 PM
chili5 said:
Why is that a bad practice?
I didn't know you could do that.
Is is better to do the static import or just type System.out.println()?
I didn't know you could do that.
Is is better to do the static import or just type System.out.println()?
Because it's basically rebinding the API into your class. It creates additional code in your class that does nothing. Also what used to happen is people would create a giant class with all their favourite functions bound into it and would always inherit from it while writing their classes. A static import means you don't have a single gigantic base class or a mess of extra variables only used for simplifying function calls.
I'd call the long version unless you do so often. The static import system is only to stop the two issues mentioned above.
#5
Posted 30 November 2008 - 03:40 PM
ahh, i see. Thanks so much for your reply and clarifying this.
#6
Posted 30 November 2008 - 03:50 PM
It's also worth noting this isn't just aesthetics. When we talk about accessing everything through a shared base class it amount to a massive amount of indirection. With Java already suffering from too many layers of indirection it doesn't make sense to add more.


Sign In
Create Account

Back to top









