0

This is my code, and I try to call the method in the inner class as shown below (the last line, ic = new oc.Inner()). But I get error.

I am using groovy console, and according to groovy documentation I expect that the Inner class can be called from outer class. I am not sure about the syntax.

class Outer {    
      private String privateStr = 'some string'  
       def callInnerMethod() {        
        new Inner().methodA()            
      }      
        class Inner { 
            def methodA() { 
                println "${privateStr}."       
            }     
       } 
}

Outer oc = new Outer()
ic = new oc.Inner() 

This is what I get as result:

startup failed:
Script1.groovy: 14: unable to resolve class oc.Inner 
 @ line 14, column 6.
   ic = new oc.Inner()
        ^

1 error
Ranj
  • 1
  • 1
  • 3

2 Answers2

1
def oc = new Outer()
def ic = new Outer.Inner(oc)

https://groovy-lang.org/differences.html#_creating_instances_of_non_static_inner_classes

Moeshin
  • 71
  • 1
  • 3
0

How about this:

def ic = new Outer.Inner()

This will likely only work if your inner class is static.

hayfreed
  • 525
  • 7
  • 21