package com.foo.bar; public class Test { abstract class X {} class X1 extends X {} class X2 extends X {} public Test foo() { return this; } public X createMessage(int n) { X x; if (n == 0) { x = new X1(); } else { x = new X2(); } return x; } public static void main(String[] args) { } }