{"id":569,"date":"2009-04-07T18:30:59","date_gmt":"2009-04-07T23:30:59","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2009\/04\/07\/java-nested-inner-class-this\/"},"modified":"2009-07-21T22:27:55","modified_gmt":"2009-07-22T05:27:55","slug":"java-nested-inner-class-this","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2009\/04\/07\/java-nested-inner-class-this\/","title":{"rendered":"Java Nested Inner Class This"},"content":{"rendered":"<p>Here is a Java certification sort of question.  Inside a nested inner class, how do you refer to the containing outer class?<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MyClass {\r\n  private String outerClassField = &quot;some value&quot;;\r\n\r\n  public void outerClassMethod() {\r\n    someObject.addMyListener(new MyListener() {\r\n      public void processEvent(MyEvent e) {\r\n        \/\/ How do you access the variable outerClassField?\r\n      }\r\n    });\r\n  }\r\n}\r\n<\/pre>\n<p>You can&#8217;t use the this keyword because that will reference the inner class!  The only way I know about, and that I have used before, is to use the class name.  Such as the following example.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MyClass {\r\n  private String outerClassField = &quot;some value&quot;;\r\n\r\n  public void outerClassMethod() {\r\n    someObject.addMyListener(new MyListener() {\r\n      public void processEvent(MyEvent e) {\r\n        MyClass.this.outherClassField;\r\n      }\r\n    });\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a Java certification sort of question. Inside a nested inner class, how do you refer to the containing outer class? public class MyClass { private String outerClassField = &quot;some value&quot;; public void outerClassMethod() { someObject.addMyListener(new MyListener() { public void processEvent(MyEvent e) { \/\/ How do you access the variable outerClassField? } }); } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-9b","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/569"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=569"}],"version-history":[{"count":1,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/569\/revisions"}],"predecessor-version":[{"id":795,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/569\/revisions\/795"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}