• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inner class

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic