dhwani: Yes, static variables are initialized when the class is loaded, which happens when the constructor is called but before the body of the constructor executes. This is why we would expect "truth Called" to be output before "Foo Const."
Bharat: I've tried this code many times on different machines (Mac and Windows), and I have not observed the odd behavior. Are you able to reproduce this? Are you able to change the behavior by switching PrintStreams?
Joined: May 08, 2007
Thanks marc for clearing my doubt,but i tried running the same program on my PC using eclipse IDE at first it printed
truth called Foo const but second time can say again and again as i was running or executing the program the order of truth called and Foo const is changing somtimes it prints
truth called Foo const
and somtimes it prints
Foo const truth called
i am a bit confused here any time can this thing affect the output of other programs as well,if this kind of strategy is being used like somewhere i use System.err and somewhere System.out? will the sequence of output change?
please if you could highlight on this point it will be kinda of you.
Joined: May 21, 2007
I have run this code many times on window.
When I use 'err' and 'out' then output is not predictable but when I use 'out' in both places output is predictable i.e truth called Foo Const
I suspect it's just the OS doing its thing. In traditional stdio implementations, stdout is line-buffered, and stderr is unbuffered, so the "dwell time" for stuff you write is going to be different -- i.e., writing newline to stderr isn't necessarily going to flush the data to the screen, but for stdout, that might very well be the case.
My prediction is that if you add
before returning from truth(), you'll see "truth called" printed first every single time.
Roughly, this means send file descriptor 2 (the error stream) to the same place file descriptor 1 (standard out) is pointing to.
"I'm not back." - Bill Harding, Twister
Joined: May 08, 2007
well!! Mr Ernest
i tried using System.err.flush(); but it is still giving the same output as explained by me earlier. Please can you tell me any reason for not printing the output in the same order? it will be kinda of you.
Normally err and out point to the same output device the screen.(you can change it though)
Now the both are buffers internally(for obvious reason of abstracting the output device from user programs)....usually the buffers are dumped when they are full...OS is conservative and does not output every byte that is written to the buffer...I think this might have to do something with the problem that our friend is facing...