Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable Shadowing

 
Peros Kalogerakos
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(){
System.out.println("Large");
}

class ReTest extends Test {
public int i = 20;
public void division() {
System.out.println("Small");
}
}

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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peros,
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic