Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problems with Inner Classes

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Problems with Inner Classes

    Hey, I'm trying to change a non-final variable when a button is clicked in my program. The problem I'm running into is that, as far as I know, you can't use non-final variables within an actionlistener which means I'm unable to alter the variable which I want to alter when the button is clicked.

    I've looked over a few google pages but so-far I haven't found anything that would give me a way around this predicament. If anyone has a solution or even a suggestion as to how to solve this problem, thanks in advance!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems with Inner Classes

    The variable must be declared final if it is only within the scope of the method creating the inner class. If it is say a class field, and this class contains a method that creates the inner class than this should be fine. I'd recommend posting an SSCCE if you are still having trouble.

  3. The Following User Says Thank You to copeg For This Useful Post:

    tyeeeee1 (December 12th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems with Inner Classes

    Can you post the code you are having problems with.
    An inner class should be able to access class variables.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (December 12th, 2012)

  6. #4
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Problems with Inner Classes

    Edit: After a bit of messing around the problem has been solved thanks to both of your comments. Thanks!

Similar Threads

  1. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 08:17 PM
  2. Need Help with using classes [HELP]
    By dragon40226 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 19th, 2011, 01:59 PM
  3. ClassLoader and inner classes
    By sidd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:07 AM
  4. Using values in different classes
    By The_Mexican in forum Java Theory & Questions
    Replies: 2
    Last Post: November 19th, 2010, 08:08 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM