13
public class Sample {

    public void method()
    {
        System.out.println("normal hai");   
    }
    public static void method1()
    {
        System.out.println("static hai");
    }
    public static void main(String[] args) {
        Sample s = null;
        s.method1();
        s.method(); 
    }
}

and the output is:

Exception in thread "main" java.lang.NullPointerException
        at com.csvfile.sample.main(Sample.java:22)

static hai

Why has the order changed? It should output:

static hai
Exception in thread "main" java.lang.NullPointerException
    at com.csvfile.sample1.main(Sample.java:22)
Diptendu
  • 2,120
  • 1
  • 15
  • 28
Ramaiah .S
  • 151
  • 6
  • @kocko Because that's the order he called the methods in... – user253751 Jun 19 '15 at 07:12
  • Calling a static method on an instance variable is bad coding style to begin with. Always call static methods on the class itself: `sample1.method1()` –  Jun 19 '15 at 07:24
  • http://stackoverflow.com/questions/1883321/java-system-out-println-and-system-err-println-out-of-order – Marvin Jun 19 '15 at 07:32
  • @Marvin: Different issue. In that question, different streams were written to. – Eric J. Jun 25 '15 at 22:25
  • @EricJ.: OP wonders about the order of his "static hai" (written to `System.out`) and his stack trace (usually written to `System.err`). I don't see how this is a different issue. – Marvin Jun 26 '15 at 00:51
  • Ah... you're right. I did not consider that the exception is written to stderr. – Eric J. Jun 26 '15 at 15:57

4 Answers4

16

The issue you have is that the Exception is printed to System.err while your code prints to System.out.

So, without a badly named class (PascalCase please) we can do:

public static void main(String[] args) throws Exception {
    final System system = null;
    system.out.println("Odd");
    System.out.println(system.toString());
}

And the output I get is:

Exception in thread "main" java.lang.NullPointerException
Odd
    at com.boris.testbench.App.main(App.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

So they're actually interleaved. i.e. the order of the output is undefined as there are two output streams being printed to the console.

Changing the code to:

public static void main(String[] args) throws Exception {
    final System system = null;
    system.err.println("Odd");
    System.err.println(system.toString());
}

Produces the desired result.

You could also catch the exception and print it to System.out to achieve the same effect:

public static void main(String[] args) throws Exception {
    final System system = null;
    system.out.println("Odd");
    try {
        System.out.println(system.toString());
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.out);
    }
}

P.S. I'm sure you know this, but you should never call a static method on an instance of the class. You should always call the static method on the class itself. So in your example, you should always do:

public static void main(String[] args) {
    sample1 s = new sample1();
    s=null;
    sample1.method1();
    s.method(); 
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • You could probably also achieve the effect by calling `System.out.flush()` before throwing the exception. – Justin Jun 19 '15 at 15:55
  • @Quincunx assuming that your terminal emulator doesn't do some other buffering and all sorts of unknowns - yes, ought to work. – Boris the Spider Jun 19 '15 at 15:56
5

That is because the exception is printed to STDERR and System.out.println() is printed to STDOUT and both streams are not synchronized.

If you call it a second time the order can change.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Jens
  • 67,715
  • 15
  • 98
  • 113
1

This is because out and err are two different output streams. However, both of them print on console. So you do not see them as different streams. Try the below code and check output.

for (int i = 0; i < 10; i++) {
        System.out.println(i);
        System.err.println(i);
}
0

Just a good to know thing in Java:

In Java there are several types of init fileds: let`s see an example:

public class HunarianEngineer{

static{
       System.out.println("1.This is a static block, called when the JVM pull in the class first time ever");
 }

{
 System.out.println("2.This is an instance block, runs before constructor");
}

public HungarianEngineer(){
     System.out.println("3.I`m a constructor");
}

}//EndOfClass

Read more about them: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html or here:

http://www.thejavageek.com/2013/07/21/initialization-blocks-constructors-and-their-order-of-execution/

narancs
  • 5,234
  • 4
  • 41
  • 60