This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class" Watch "Inner class" New topic

Inner class

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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
Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
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

Joined: Mar 02, 2005
Posts: 580
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
jQuery in Action, 3rd edition
subject: Inner class
It's not a secret anymore!