File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inner Class inside a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Class inside a method" Watch "Inner Class inside a method" New topic
Author

Inner Class inside a method

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

I am trying to access the variable inside the method by the Inner Class object inside the same method.Is't possible?? I am getting error for the below program. It's my own coding, so how to do it correctly.

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Could you copy and post the error message you get? That would make it faster.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

You can do this -- but the JLS says that those methods that you access must be final.


Problem is, your code still can't compile because Inner1 is not a member of the class Outer1 -- it's just a local class in your method. (I think it very odd that the compiler allows this, as I can't think of a useful reason for doing this.)

Remove the amethod() declaration and this should work:



Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
But I am still getting the error message as follows:

Coding I used:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3492
    
  13
Originally posted by Mark Henryson:



That line should be


Joanne
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41600
    
  55
Joanne Neal wrote:

Outer1.Inner1 in = new Outer1().new Inner1();


I have never seen this notation - has that always been possible?
It's not exactly intuitive or reader-friendly, is it?


Ping & DNS - my free Android networking tools app
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3492
    
  13
It does work.
I was just correcting the OP's code. I agree there are more reader friendly ways of writing it.
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

Thanks for the reply Joanne Neal,Joel McNary and all.

I want the example coding for the below thing given in the code.
We didn't used the variable as final, but we are able to access it. Something I am missing??

Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Any variable declared inside a method lives in the stack and has method scope.
Which means once the method is completed you can no longer access the variables.
So the declaration of "i" inside the method cannot be used by the class, as you might pass a reference of the class elsewhere and it cannot access the local variables.

But declaring the variable as final makes the variable a "compile-time constant" and hence they can be referenced outside.


class A{

//int i = 12; no compiler error
public void amethod()
{

int i = 12;

class Inner1
{

public void bmethod()
{
System.out.println(i);
}
}

}
}


If you are not laughing at yourself, then you just didn't get the joke.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ulf Dittmer:
I have never seen this notation - has that always been possible?


Since Java 1.1, which introduced inner classes, if I remember correctly.

It's not exactly intuitive or reader-friendly, is it?


What would be a better notation?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Joel McNary:
a local class in your method. (I think it very odd that the compiler allows this, as I can't think of a useful reason for doing this.)


I use that feature for Visitor implementations that need to return (or rather collect) a value:



Works well for me...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner Class inside a method