Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a good reason why you can or cannot have an inner interface within a Java class?
    primarykey
    data
    text
    <p>Is there a good reason why you can or cannot have an inner interface within a Java class? (like you would an inner class). I couldn't find a definitive answer with Google, but it appears as though you can't embed an interface the same way you would with an inner class. My guess is that the Java creators didn't see a great reason to make it possible, and so it isn't but maybe there are really good reasons?</p> <p>for example I can't get this to compile (it's all one big class)</p> <pre><code>package buttonGetSourceObject; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class ClassWithButton implements ActionListener, ButtonOwner{ private String objectName = ""; public ClassWithButton(String string) { objectName = string; JFrame f = new JFrame(); JPanel p = new JPanel(); MyJButton b = new MyJButton(this,"Press This Button for Name of Owner of Button"); b.addActionListener(this); f.add(p); p.add(b); f.pack(); f.setVisible(true); } public static void main(String[] args){ ClassWithButton c = new ClassWithButton("object1name"); } @Override public void actionPerformed(ActionEvent arg0) { ((ButtonOwner)((MyJButton)arg0.getSource()).getOwner()).printInstanceName(); } public interface ButtonOwner { public void printInstanceName(); } private class MyJButton extends JButton { /** * */ private static final long serialVersionUID = 1L; Object owner = null; public Object getOwner() { return owner; } public void setOwner(Object owner) { this.owner = owner; } public MyJButton(Object o, String string){ owner = o; this.setText(string); } } @Override public void printInstanceName() { System.out.println(this); } public String getObjectName() { return objectName; } public void setObjectName(String objectName) { this.objectName = objectName; } public String toString(){ return this.getObjectName(); } } //end </code></pre> <p>the compiler doesn't seem to recognize the inner Interface "ButtonOwner" as existing at all</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload