• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Class inside a method

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you copy and post the error message you get? That would make it faster.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I am still getting the error message as follows:

Coding I used:

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Henryson:



That line should be
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??

 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}

}
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic