Hi suren, On Jan 31, 2:49 pm, suren <[email protected]> wrote: > package Chapter8; > > public class StaticMethodLocalInnerClass { > public static void main(String arg[]) > { > Outer2 outOb=new Outer2(); > outOb.doStuff();} > } > > class Outer2{ > static int x=10; > int y=100; > static void doStuff() > { > > class Inner2{ > int a=1111; > public Inner2 doInnerStuff() > { > System.out.println(x); > return this; // this is possible > } > } > Inner2 in=new Inner2(); > in.doInnerStuff(); > //return this.x; // can not use 'this in static method > } > > } > > why this keyword is not possible in static method whereas it is > possible in class inside that static method?
A static method of a class can be called with <Class>.<static method> (), which means, there is no instance of the class available when you call the method and as a result of this, there is also no "this" reference available. The "this"-Reference is no special case of the inner class. Your Inner2-Class is defined like any other class. It has a method which uses the reference to itself. The method doInnerStuff() of Inner2 just returns the reference to the actual instance of Inner2. As in the code of class Outer2/method doStuff(), the commented "this" and the "this" in the Inner2-Class are not the same references!!!! HTH Ewald -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
