aspose file tools*
The moose likes Beginning Java and the fly likes Can anyone explain this please ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can anyone explain this please ??" Watch "Can anyone explain this please ??" New topic
Author

Can anyone explain this please ??

Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
Please take a look at following code.

class Foo {

static boolean truth() {
System.err.println("truth Called");
return true;
}

static final boolean TRUTH = truth();

Foo() {
System.out.println("Foo Const");
}
}

public class Test {
public static void main(String args[]) throws Throwable {
new Foo();
}
}


When I ran this code, it had produced following output(Which I was Expecting)

truth Called
Foo Const

When next time I ran that code it had produced following output(Which I was not expecting)

Foo Const
truth Called

Is output is buffered before it gets printed on console ???

[ June 24, 2007: Message edited by: Bharat Makwana ]
[ June 24, 2007: Message edited by: Bharat Makwana ]

ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

SCJP1.5, SCWCD1.5
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Something to do with the different PrintStreams (System.err and System.out)...?

If this behavior is reproducible for you (it's not for me), then you should be able to determine if it's the PrintStreams by switching err and out.
[ June 25, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
Thanks you Mr.weber
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!
please if anyone can tell me if i am right here,in the below shown code,the class test calls the the constructor of Class Foo
so it prints Foo const

class Foo {

static boolean truth() {
System.err.println("truth Called");
return true;
}

static final boolean TRUTH = truth();

Foo() {
System.out.println("Foo Const");
}
}



public class Test {
public static void main(String args[]) throws Throwable {
new Foo();
}
}



but it prints "truth called" is it because it is in static block it is called automatically as the class is instantiated?

please let me know if i am correct or there is some other reason for it?
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
I do not understand how will using err or out will affect output.

I think it should be,

truth called
Foo const

regardless of what I use , err or out
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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.
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
hi marc.

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

And with 'err' and 'out',sometimes it's

Foo Const
truth called

and sometimes
truth called
Foo Const

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Bharat Makwana:
... When I use 'err' and 'out' then output is not predictable but when I use 'out' in both places output is predictable...

It seems like there's some multithreading going on, but I just don't know. I couldn't find anything on this last night. I'll do some more looking.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

System.err.flush()

before returning from truth(), you'll see "truth called" printed first every single time.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I've been on at least one system where flush() didn't seem to fix this problem. You may need to write everything to the same stream. E.g. replace each System.err with System.out. Or call

System.setErr(System.getOut());

at the beginning of your program. Or use the command line to redirect error to standard out. This seems to work on both Windows and Unix-based systems (those I've tried, anyway).

java MyProgram 2>&1

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
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

What OS and Java version are you using?

(My thanks to EFH and Jim for joining in. )
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok!!!
i am using windows XP operating system and java 1.6 version i am using....
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
I think what EFH is saying could be the reason.

According to my knowledge....

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anyone explain this please ??