• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local Inner class

 
Venkat Ramanan
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is giving me an error...
class LocalclassDemo
{
static int classvar = 50;
public static void main(String args[])
{
final String localvar = "ocal variable accessed";
class Local
{
int classvar = 100;
Local()
{
System.out.println(LocalclassDemo.this.classvar);
System.out.println(classvar);
System.out.println(localvar);
}
}
new Local();
}
}
I am trying to access the LocalclassDemo class variable classvar from within the class Local using the following statement....
System.out.println(LocalclassDemo.this.classvar);
But it is throwing me error....
can anyone explain How to access this variable from the inner class Local?
Rgds
Venky
 
Pratiti Naphade
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the reason is u canot use 'this' from within a static method.It does not give an error if u try enclosing a class in a non static method.
 
Wong KK
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simply use LocalclassDemo.classvar
 
ego hu
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I think it should have another error.
It can not be declared "new Local();".
It must be " new LocalclassDemo().new Local()";
Am I right?
 
Abhijit Rayarikar
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ego,
You dont have to declare " new LocalclassDemo().new Local()" since the only variable in LocalclassDemo that you are acessing is static, so it does not make any difference if the outer class has been instantiated or not.
If that was not static, then you would be right.. you would have to declare it as " new LocalclassDemo().new Local()".
-Abhijit
 
Bob Vel
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,
I compiled this code and I got this error message.
Cannot make static ref to to non-static variable classvar.
Local not found in void main(java.lang.String[])
Local not found in type declaration

class LocalclassDemo
{
int classvar = 50;
public static void main(String args[])
{
final String localvar = "Local variable accessed";
class Local
{
int classvar = 100;
Local()
{
System.out.println(LocalclassDemo.classvar);
System.out.println(classvar);
System.out.println(localvar);
}
}
new LocalclassDemo().new Local();
}
}
------------------
Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.
No involvement, no commitment.
 
shabbir zakir
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
Why this code is giving problem
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have all of this Local stuff in main, which is a static method.
The solution is to pull the Local stuff out of main.
<pre>
class LocalclassDemo
{
int classvar = 50;
public static void main(String args[])
{
final String localvar = "Local variable accessed";
LocalclassDemo dem = new LocalclassDemo();
LocalclassDemo.Local t = dem.new Local();
System.out.println(dem.classvar);
System.out.println(t.classvar);
System.out.println(localvar);
} //main ends here

class Local
{
int classvar = 100;
Local()
{
//System.out.println(LocalclassDemo.classvar); This won't work cuz you have hidden the classvar variable.
System.out.println(classvar);
//System.out.println(localvar); This won't work cuz localvar is local to main and we pulled this out of main.
}
}
}
<pre>

[This message has been edited by Cindy Glass (edited March 22, 2001).]
 
Bob Vel
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it is clear.
Thanks Cindy.
------------------
Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.
No involvement, no commitment.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic