aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local Inner class" Watch "Local Inner class" New topic
Author

Local Inner class

Venkat Ramanan
Ranch Hand

Joined: Jan 25, 2001
Posts: 30
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

Joined: Mar 13, 2001
Posts: 39
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

Joined: Jan 15, 2001
Posts: 52
simply use LocalclassDemo.classvar
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
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

Joined: Mar 21, 2001
Posts: 3
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


This message consists entirely of recycled electrons. No electrons were harmed during the creation of this message
Bob Vel
Greenhorn

Joined: Mar 21, 2001
Posts: 24
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.


Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi guys
Why this code is giving problem
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bob Vel
Greenhorn

Joined: Mar 21, 2001
Posts: 24
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
 
subject: Local Inner class