Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class

 
Parameswaran Thangavel
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.


1)



2)






3)





4)

is it possible to use the this keyword inside the constructor
 
Baiju Scariah
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...
 
vidya sagar
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)


static
{
int x;
outer o=new outer();
o.outermethod();
}

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


4

Very well we can use this in constructor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic