Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Class inside a method

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1361
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you copy and post the error message you get? That would make it faster.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I am still getting the error message as follows:

Coding I used:

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Henryson:



That line should be
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}

}
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic