How to return object of private inner class object java

Go To StackoverFlow.com

0

How can I return the object of a private inner class

public class MainClass{
   public InnerClass getObj(){
       return this.new InnerClass()
   }
   private class InnerClass{
       InnerClass(){
       }
   }
}
-------------
MainClass mc = new MainClass();
mc.getObj(); // assume I am not holding it anywhere.

The above code is giving a null pointer exception, when I checked with the try-catch blocks. Also, when tried in debug mode, its says 'ClassNotFoundException'

But I can see the class file generated as MainClass$InnerClass.class

2012-04-05 23:50
by sravanreddy001
I can see that the constructor of the InnerClass is not being called. I tried changing access modifier to public/none, its still same behaviou - sravanreddy001 2012-04-05 23:55
The return type of getObj() is set to void so you won't be able to return anything. What should the correct return type be - Pradeep Gollakota 2012-04-05 23:55
Thanks for pointing, i edited the code, its InnerClas - sravanreddy001 2012-04-05 23:59
Write a SSCCE for us and post it - Mike Clark 2012-04-06 00:01
There was actually, some mistake in the code, I thought it was some access related to InnerClass, but after doing a proper debugging, the issue is some thing else. if I provided the actual code, it would have been easier, but.. its clear now. - sravanreddy001 2012-04-06 00:52


2

Hmmm, I'm not having that same problem. Here's my modified version of your code:

class MainClass {
    public static void main( String[] args ) {
        MainClass mc = new MainClass();
        InnerClass ic = mc.getObj();
        System.out.println( ic );
    }

    public InnerClass getObj() {
        return this.new InnerClass();
    }

    private class InnerClass {
        InnerClass() {}
    }
}

The result of this code is:

MainClass$InnerClass@64c3c749
2012-04-06 00:02
by Ryan Nelson
I'm not sure why its not working in my project(little diff code), but your code when i copied and ran, its working fine.. I will try to edit the question properly (after reviewing), if not.. will mark yours correct - sravanreddy001 2012-04-06 00:07
In this case main can see the private type since it is in the same class. If main were in a different class it would not wor - Baruch 2014-06-17 11:52


1

As others have already pointed out all you need to do is fix the return type of your method.

However, you should be aware that you'll get a warning since you're exporting a non-public type in a public method.

2012-04-06 00:06
by Pradeep Gollakota
he complains of a NullPointerException, which cannot arise as the result of marking 'void' a method which contains a 'return'. This is because the 'void'/'return' conflict is a compile-time error, and NullPointerException is a runtime exception. How could he get a runtime exception if his posted code can't get past compiletime? The intuitive answer is: he posted the wrong code, the 'void'/'return' problem does not exist in his actual codebase, and his problem still remains. : - Mike Clark 2012-04-06 00:11
Quite right! However, with the flood of answers that were coming in, I at least wanted to bring attention to the fact that the provided example code is exposing non-public types in a public API. We really can't debug and help if wrong code is posted. +1 for this and +1 for SSCC - Pradeep Gollakota 2012-04-06 00:18


1

Since your getObj() is public, you should either keep the inner class private and return an Object, or make it public and return an InnerClass, depending on your needs.

The following code returns an object in example:

public class MainClass {
        private class InnerClass {
                InnerClass() { System.out.println("InnerClass"); }
        }

        public Object getObj(){
                return new InnerClass();
        }

        public static void main (String[] argc) {
                MainClass a = new MainClass();
                System.out.println(a.getObj());
        }
}

It prints out:

bash-4.2$ java MainClass
InnerClass
MainClass$InnerClass@53c059f6

As you can see there are no problems with the constructor.

2012-04-06 00:08
by Riccardo T.


0

You simply forgot the return type of your getObj method.

 public InnerClass getObj(){return new InnerClass();}
2012-04-06 00:01
by NoName
edited it, but return new InnerClass() will not work, this.new InnerClass() must be used - sravanreddy001 2012-04-06 00:09
Ads