• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question on Local Classes in java

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I have the following code.

Can any one please explain the reason for compilation erorrs at Q1 and Q2.
Q1:
Why only final variables need to be passed as formal paramter to the method?

Q2:
Why i cannot make use of TOPLevelClass.this.nsf to access the field in the outer class.

Q3:
can we use this keyword in a class which is declared inside the static method.

Thanks,
Ravindra.



--- indenting repaired, Bu.
[ July 07, 2007: Message edited by: Burkhard Hassel ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sliding this back to the scjp forum
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ravindranath Chowdary:
Q1:
Why only final variables need to be passed as formal paramter to the method?

Did you mean this as general question or based on your example? Based on your example, there is no reason why fp must be final.

Q2:
Why i cannot make use of TOPLevelClass.this.nsf to access the field in the outer class.

It's the same reason why you can't use nsf in the static method. Inside a static method, there is no instance of the class available.

Q3:
can we use this keyword in a class which is declared inside the static method.

An inner class is like any other class. As result, you have a this variable which points to the current instance of the StaticLocalClass.
[ July 07, 2007: Message edited by: Manfred Klug ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1
<conjunctive>If fp had to be used inside of the method local class, it had to be final. Otherwise the method local class had no access to it.</conjunctive>
Because fp is a method local variable that lives on the stack. If it weren't final, it might be dumped already when an object of the inner class is still alive.
But if the parameter variable fp isn't really used, as Manfred wrote, it doesn't really needs to be final.

Q2
as Manfred wrote. The method is static an nothing in it has access to non-static members of the outer class.

Q3
see Manfred again. The inner class itself is not static, simply because there is no such a thing as a static class (the so called static inner classes are not static by themself, but they are static members of the outer classes).
And there is no use of this inside a static method. The this is used in the constructor of the inner class.
As from a constructor, a non-static variable (j) has access to a non-static (& non-private) variable (nsf) of the super class.

Yours,
Bu.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic