aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessing local variables from inner classes in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "accessing local variables from inner classes in Java" Watch "accessing local variables from inner classes in Java" New topic
Author

accessing local variables from inner classes in Java

henry joe
Greenhorn

Joined: Jul 24, 2012
Posts: 8
The code below complains about me assessing x in an inner class, what must I do to get the code working? Also, there is a complain on the TCIP ip = new TCIP() statement.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

I suppose you are referring to a compiler error message? What exactly does that message say?
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
henry joe wrote:The code below complains about me assessing x in an inner class, what must I do to get the code working?

Mark x as a final variable. Because Method-inner class can not access local variables of the enclosing method. But it can access final variables...

henry joe wrote:Also, there is a complain on the TCIP ip = new TCIP() statement.

Because you have access the class before its definition. So do the following:

Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

If you're curious as to why the compiler requires the variable to be marked final, have a look at Local Inner Classes here -:

Suhrid's Wiki : Inner Classes


OCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing local variables from inner classes in Java
 
Similar Threads
Questions on inner class & cast down
Inner Class
Question from vodoo exam
Inner Class
Static Inner Class