Home arrow static arrow Java Programming [Archive] - Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var
This topic has 3 replies on 1 page.

Posts:91
Registered: 4/21/04
Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var  
Aug 9, 2004 9:53 PM



 
i fail to make an inner class distinguish among function parameter and outer class variable.

consider the following code:

public class a {
int var; // i want to access this one using inner class.

public a(int var) {
this.var = var;

class b {
public void fun() {
var = 100;
}
}
}
}

i get an error a.java:9: local variable var is accessed from within inner class; needs to be declared final.

when i change the code to this:

public class a {
int var; // i want to access this one using inner class.

public a() {
class b {
public void fun() {
var = 100;
}
}
}
}

it compiled no problem.it seems that inner class is accessing function parameter rather than outer class member.

is there any syntax i can use to make me access the outer class member variable whithout renaming or removing the function parameter?

thank you.
 

Posts:18,384
Registered: 21.03.00
Re: Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var  
Aug 9, 2004 10:17 PM (reply 1 of 3)



 
Hi,

Yes there is:
a.this.var = 100; //Use this in the inner class.

/Kaj
 

Posts:5,965
Registered: 5/17/03
Re: Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var  
Aug 9, 2004 10:19 PM (reply 2 of 3)



 
Do you have to declare class b in the constructor of a? If you put it outside it works.
 

Posts:5,965
Registered: 5/17/03
Re: Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var  
Aug 9, 2004 10:21 PM (reply 3 of 3)



 
a.this.var = 100; //Use this in the inner class.

Amazing syntax -:)
 
This topic has 3 replies on 1 page.