YankeeWhiskey
YankeeWhiskey

Reputation: 1582

Java call a function outside a class

Is there a way to call a function outside a class? I don't know how to say it but I have an example.

class Class1{
    class Class2{
          public void callFunctionInC1(){
                //how can I call funtionNeedtoBeCalled
          }
    }
    public Class1(){
          Class2 c2 = new Class2();
          c2.callFunctionInC1();
    }
    public void functionNeedtoBeCalled(){
          //do something
    }
 }

It sounds weird but I do have a reason to do that. Is there anyway to do that? Thanks.

Upvotes: 1

Views: 4631

Answers (2)

Frank Sposaro
Frank Sposaro

Reputation: 8531

Class1.this.functionNeedtoBeCalled();

Here is a link with some more discussion. http://www.velocityreviews.com/forums/t137884-inner-class-explicit-outer-class-method-call.html

Calling outer class function from inner class

Upvotes: 8

Thomas Uhrig
Thomas Uhrig

Reputation: 31605

Yes: Class1.this.functionNeedtoBeCalled();

Upvotes: 4

Related Questions