Results 1 to 6 of 6
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Problems with Inner classes

    I understand inner classes and have used them a bit before(mostly for actionhandling)

    This problem deals with an innerclass for actionhandling on a calculator. I originally had lots of repetitive handling as anonymous inner class where only one thing changes on each button.

    I switched to code to this, which allows me to specify which number to display
    Java Code:
    public class Calculator{
      //create button
      class NumberHandling implements ActionListener{
    			String number;
    			
    			private NumberHandling(String number){
    				this.number = number;
    			}
    			
    			public void actionPerformed(ActionEvent e){
    				if(clearView){
    					view.setText(number);
    					clearView = false;
    				}
    				else{
    					StringBuilder str = new StringBuilder(view.getText());
    					str.append(number);
    					view.setText(str.toString());
    				}
    				displayLabel.setText(displayLabel.getText() + number);
    			}
    		}
    }
    My problem is the fact that I have to make the inner class be
    Java Code:
    class ClassName implements ActionListener{
    When I tried
    Java Code:
    private class ClassName implements ActionListener{
    I received an illegal start of expression error.

    When I changed it to
    Java Code:
    private static class ClassName implements ActionListener{
    I got 100 errors.

    Im just wondering why it's complaining about having the inner class having any sort of access modifier.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Im just wondering why it's complaining about having the inner class having any sort of access modifier.
    There must be something else you're doing wrong. Inner classes can be private, protected, package scope or public.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    ugh. Stupid mistakes >< I had the inner classes inside a method.

    But how come I can create an inner class inside a method if it has package access, but not if it has private or public?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    ugh. Stupid mistakes >< I had the inner classes inside a method.

    But how come I can create an inner class inside a method if it has package access, but not if it has private or public?
    The JLS wants it that way:

    Quote Originally Posted by JLS
    14.3 Local Class Declarations
    [ ... ]
    It is a compile-time error if a local class declaration contains any one of the following access modifiers: public, protected, private, or static.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 02:29 PM
  3. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 02:31 PM
  4. Help with classes
    By freswood in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 03:28 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •