• 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

Method Local Inner classes

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inner class object cannot use the local variables of the method the inner class is in.

Because the local variables aren't guaranteed to be alive as long as the method-local inner class object, the inner class object can't use them. Unless the lcoal variables are marked final.



 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question to add to this :

Page 672 K&B :

"You can't for example mark method-local inner class public,private, protected, static, transient and the like. -- Why so ?

For the purpose of the exam, the only modifiers you can apply to a method-local inner class are abstract and final".
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Since the variable is final in the method, so the local class gets a copy of the variable. When an instance of the local class is created, it gets a copy of the final variables of the enclosing method. There is no problem with this as we know that the value of final variables cannot be changed, so the copy of the final variable that the local class has is always same as the value of the final variable in the method.
2. What is the use of marking any declaration inside a method private or protected or static?? The declarations in a method are available only inside the method, so marking them public or private is useless...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to run your code with the outer variable 'x' as final and didn't get any error.?


got this output:
Outer x is :Outer2
local variable z is:local variable

You cannot access a variable inside method unless its final, since in that case each method invocation will have to keep its own instance of local variable.

Correct me if I am wrong.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 years and yet no satisfactory reply for this question
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek Madhusudhan wrote:3 years and yet no satisfactory reply for this question



Can you elaborate which of the questions in this discussion that you have having issues with. And exactly what is the issue?

Henry
 
Abhishek Madhusudhan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am getting the output as 10. There are 2 possibilities here:-

1. It might be that there might be an error in SCJP book. (As according to the book only if the variable is declared final then the method should be able to access else it should show an compile time error).
2. There might be something to do with compiler version that i may not be aware of ??

The above op's too haven't got errors regarding the code.

Got any leads on this
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek Madhusudhan wrote:
1. It might be that there might be an error in SCJP book. (As according to the book only if the variable is declared final then the method should be able to access else it should show an compile time error).
2. There might be something to do with compiler version that i may not be aware of ??



Basically ... Java 8 changed the rules (a little bit) regarding the "final" requirement of local variables and inner classes. It just now have to be effectively final, and not actually declared as final.

So, unless your book is for Java 8, and you are taking SCJP for Java 8, there is nothing wrong with your book.

Also ...

Abhishek Madhusudhan wrote:3 years and yet no satisfactory reply for this question



Since Java 8 didn't exist 3 years ago, there is nothing wrong with this topic either...

Henry
 
Abhishek Madhusudhan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well that explains something. my bad i saw no answers posted since the last 3 years so i just took it as no.

tell me what exactly do you mean by effectively final and not actually declared final, i don't want to hijack the thread but have one more question here

In Anonymous Inner classes - method based - declared in argument - whats the use of declaring the class apart from passing an instance of interface to just make the method( which accepts an instance of interface )to be executed or in short, what's the use of argument based anonymous inner class.

Thanks in advance
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek Madhusudhan wrote:
tell me what exactly do you mean by effectively final and not actually declared final, i don't want to hijack the thread but have one more question here



The term "effectively final" is actually used by the Java specification. I believe the Java compiler mentions it in some of the error messages as well. You can probably Google that term, and get a few good blogs regarding it.

Abhishek Madhusudhan wrote:
In Anonymous Inner classes - method based - declared in argument - whats the use of declaring the class apart from passing an instance of interface to just make the method( which accepts an instance of interface )to be executed or in short, what's the use of argument based anonymous inner class.



Can you elaborate? Or maybe even give an example of what you are referring to? Not sure what you are asking.

Henry
 
Abhishek Madhusudhan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i mean was



this is what i meant but i don't get as to where exactly this is going to come in handy though i wrote this example myself :P
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek Madhusudhan wrote:
this is what i meant but i don't get as to where exactly this is going to come in handy though i wrote this example myself :P



So, basically, (1) you took created an anonymous inner class, (2) using it to implement an interface, (3) declared an method that takes that interface, and (4) and called the method ... all in one line. And you want to know when such a use case comes in "handy"?

How would we know such an answer? First, it is very easy to mix and match different features to create something that is never going to be used. Second, we don't know what type of coding you are doing. If all you are doing is writing simple codes, infrequently, that is shared for work, I would gather that something like this, which would be difficult to read for your colleagues, would be a really "non-handy" idea.

Henry
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I write my Swing listeners as method-local inner classes a lot, just like that. For example:



Or now with Java 8 lambdas where you don't even have to mention the interface name or the name of its single method:


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic