aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

local class

faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
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;


}
}

}
}
Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
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

Joined: Oct 17, 2000
Posts: 173
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)
jon c
Greenhorn

Joined: Sep 29, 2000
Posts: 4
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: local class