wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Variable Shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable Shadowing" Watch "Variable Shadowing" New topic

Variable Shadowing

Peros Kalogerakos

Joined: Jan 30, 2002
Posts: 7
Another problem in the following code, at the section I have commented as //***** the compiler throws the following error ..."'this' cannot be referenced from a static context".
Could anyone explain what I am doing wrong? Please.

public class Test {
protected int i = 10;
protected void division(){

class ReTest extends Test {
public int i = 20;
public void division() {

public static void main (String args[]){
Test FirstTest = new Test();
ReTest SecondTest = new ReTest(); //******
Test ThirdTest = SecondTest;
FirstTest.division(); //Large
ThirdTest.division(); //small
System.out.println(ThirdTest.i); //20
System.out.println(FirstTest.i); //10
Peros Kalogerakos

Joined: Jan 30, 2002
Posts: 7
If I create another class and make it public and place the main method and related calls in that class then it works.
Don't get what is going on here - why can't it see the ReTest class??
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
I tried compiling ur code. I get an error saying
"No enclosing instance of class Test in scope ......." at the same commented line.
If u know inner classes, the first two soultions wud work.
ReTest is in the current position an inner class to TEst Class.
1. U cud change the error giving statement to

Test.ReTest SecondTest = new Test().new ReTest(); //******

Now the inner class ReTest is properly instantiated.
2. Or u cud introduce a "static" keyword as a modifier to ReTest class definition.
3> Or u cud just move out the whole ReTest class
portion beyond and after the main() method.
This wud also work fine. Now they are just two distict classes .

public class Test {
public static void main(String args[]){
class ReTest extends Test{

Hope this helps!
[ January 31, 2002: Message edited by: Jennifer Wallace ]
[ January 31, 2002: Message edited by: Jennifer Wallace ]
[ January 31, 2002: Message edited by: Jennifer Wallace ]
Peros Kalogerakos

Joined: Jan 30, 2002
Posts: 7
Thanks for the response.
I am actually studying from the Mughal PGJC book - so I haven't go to inner classes yet (its the next chapter).
Regarding your #2 - "static modifier".
I wasn't aware that a class can be static?? Isn't a class an object? I thought only variables and methods could be "static" ??
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Personally I think that you just typoed on the code. You left the final bracket out of the first class, causing the second class to be an inner class of the first.
Fix that and THEN see what your problems are.
(If you have not studied inner classes, then you do not need to get confused about static inner classes - which are actually called top-level nested classes - a little nitpik in the Java Language Specification.)
wood burning stoves
subject: Variable Shadowing
Similar Threads
Designing a class and test program
Object Reference Null
Finalize() method question
try-catch problem
what is the concept behind?