• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Inner class

Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i just worked around some sample code and i just

pasted it, the doubt i got is given by the

commented line.just try explain its behaviour.





is it possible to use the this keyword inside the constructor
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you to study inner classes and ask doubt sepecifically rather than giving 3 or 4 programs and asking us to exaplin that...
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi paramesh

Code 1:

class stack
stack ss = new stack();
public static void main(String[] as)
new stack();

This normally throws stack overflow because when we create a object, JVM allocates space for reference ss and try to create a new space for its object, which it tends creates new space for its reference and try to assign its object for it...
so this way it going end up with a stack overflow error

In the code 2(part of your code)

int x;
outer o=new outer();

whenever a variable is declared in a block its scope ends when block ends

In the code 3(Part of your code)

public void innermethod()
outer oo=new outer(); //1
oo.outermethod(); //2
oo.y=5;//whats the difference between //3
System.out.println(outer.this.y);//these two lines // 4
System.out.println(oo.y); System.out.println("innerrmethod");

5 is instialized to value oo u created in line 2.
But outer.this.y refers to currently running object of outer class so it prints 2


Very well we can use this in constructor
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!