2

I am trying to creating a customized JButton. I noticed that when I declare the class class MyButton extends JButton, it works ok. But Eclipse shows "illegal modifier" error when I changed the class name to private class MyButton or protect class MyButton.

I understand that only one class can be declared as public in one file, but what's wrong with declaring it as a private class? Can someone help to explain how the different modifiers work for the classes in this case? Thanks!

class MyButton extends JButton {
@Override
   protected void paintComponent(Graphics g){
        Graphics2D g2d =(Graphics2D)g; 
        g2d.setColor(new Color(200,50,50,255));
        g2d.fill(new RoundRectangle2D.Float(0,0,getWidth(),getHeight(),100,100));   
   }
}
 public class CustomButton extends JFrame{
   public CustomButton (){
      add(new MyButton());
   }
   public static void main(String[] args) {
       CustomButton b = new CustomButton();
       b.setVisible(true);           
   }    
 }
  • I don't see an inner class in your example...not to mention that you won't see any text or icons with your `MyButton`... – MadProgrammer Sep 19 '14 at 02:13
  • That's not an inner class! Inner classes should be inside the parent class - your `CustomButton` class is outside. – Ken Y-N Sep 19 '14 at 02:18
  • Thanks guys for pointing that out! I was confused with that definition and class modifiers. I have edited the question. Hope it reads better now. – stillAFanOfTheSimpsons Sep 19 '14 at 02:26
  • Nope, still don't see an inner class here...perhaps you should have a look at [Classes and Objects, inner classes](http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) – MadProgrammer Sep 19 '14 at 02:47
  • Yea I mean I figured out two classes in one file does not equal to "one of them is an inner class" . Inner class refers to one private class inside a public class, I reckon. I modified the question, and I guess I should phrase the question as: why can't we declare public and another "private, protected" class in one file. – stillAFanOfTheSimpsons Sep 19 '14 at 02:55

2 Answers2

4

The only access modifiers that are allowed for top-level classes are:

  • public, making it accessible from anywhere
  • (default/none), making it package-private, i.e. only visible to classes in the same package

From the Java language specification:

The access modifier public (§6.6) pertains only to top level classes (§7.6) and to member classes (§8.5), not to local classes (§14.3) or anonymous classes (§15.9.5).

The access modifiers protected and private (§6.6) pertain only to member classes within a directly enclosing class or enum declaration (§8.5).

And also:

It is a compile-time error if a top level type declaration contains any one of the following access modifiers: protected, private, or static.

So, if you want two declare multiple top-level classes in the same file, one of them (the one matching the source file name) will have to be marked public, while the others will need to have default (package-private) access.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Thanks! or other wise I can declare one public class in a file and nested private classes inside it, and it won't affect their behaviors? – stillAFanOfTheSimpsons Sep 19 '14 at 03:12
  • You can, but be aware of the effects of declaring them `public`/`protected`/`private`. Also a `static` member class behaves different from a non-static one. The spec or any decent Java book will give you all the details. – Robby Cornelissen Sep 19 '14 at 03:16
0

Though there are 4 access modifiers in Java (public, protected, default, private), you can only use either public or default as access modifiers to the outer most classes (Top level classes)

You can use other access modifiers if the class is an inner class

Think about this. If you make a top most class private and if java allows it, what is the benefit that you can get from that? Since private stuffs can't be access by anywhere else other than by itself there is no point of allowing private access modifier for the outer most class