• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Local Inner class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simply use LocalclassDemo.classvar
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys
Why this code is giving problem
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic