Results 1 to 6 of 6
Thread: Problems with Inner classes
- 03-11-2011, 02:04 PM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
My problem is the fact that I have to make the inner class beJava 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); } } }
When I triedJava Code:class ClassName implements ActionListener{
I received an illegal start of expression error.Java Code:private class ClassName implements ActionListener{
When I changed it to
I got 100 errors.Java Code:private static class ClassName implements ActionListener{
Im just wondering why it's complaining about having the inner class having any sort of access modifier.
-
It shouldn't be causing this error. Can you create an SSCCE that shows the error?
- 03-11-2011, 02:34 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
- 03-11-2011, 03:21 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 03-11-2011, 03:27 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
- 03-11-2011, 03:30 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
Help with classes
By gnarly hogie in forum New To JavaReplies: 14Last Post: 10-10-2008, 02:29 PM -
Cant run my classes
By Assaf A in forum EclipseReplies: 1Last Post: 04-22-2008, 02:31 PM -
Help with classes
By freswood in forum New To JavaReplies: 5Last Post: 04-21-2008, 03:28 PM -
When do we use inner classes?
By cruxblack in forum New To JavaReplies: 5Last Post: 08-10-2007, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks