• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

local class

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With local class...one can also access static var of the enclosing class inside local class...but here it gives an error with `"a"...???
why???

class superB{
private double x;
private static double y;
class enclosingA extends superB{
private double z;
private static double a;
void nonstmethod(final int i){
final int j;
int k;
class nonstlocalD extends superB{
double yy= y;
double xx= x;
double zz=z;
double aa=a;
int ii=i;
int jj=j;


}
}

}
}
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler rejects the variable "a" because the variables cannot be static inside inner class or any local method. only interface variables and the top level classes member variables are permitted to be static.
This is my understanding. Please let me know if i am wrong.
class superB{
private double x;
private static double y;
class enclosingA extends superB{
private double z;
private static double a; // -- Gives a compiler
// error at this line.
void nonstmethod(final int i){
final int j;
int k;
class nonstlocalD extends superB{
double yy= y;
double xx= x;
double zz=z;
double aa=a;
int ii=i;
int jj=j;


}
}

}
}
Sunitha. S
[This message has been edited by Sunitha Sounderrajan (edited September 28, 2000).]
 
faiza haris
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enclosing class can declare both static and non-static variables.
A local class when in a non static context can take both (static and non-static ) variables of the enclosing class.
Since the method is nonstatic i dont find any reason for it not taking the static variable.
I know only Toplevel classes can be static, but local,anonymous inner classes can occur in a static context making them implicitly static.
It works...as the book says..but gives compile error...
(Ref Khalid�s book)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner class cannot have non-final static variables. (but some rule applies again for final static variables)
* Inner class can have final static variables.
static final int i = 10;
static final String s = "hello";
* But the following is wrong ( the static final variable cannot be initialized using an expression. "new" in the following code)
static final String s = new String("hello");
static final Integer ii = new Integer("12");
static String ss = "testing"; // not final
(Please Refer the inner class specs)
Please let me know if am wrong ..
[This message has been edited by jon c (edited September 29, 2000).]
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic