why inner class can use outer class function ?

A

Ahan Hsieh

Sir:
I use NetBeans to add a button listener (e.g. mouseClicked, see below),
but I found one starnge thing is the mouseClicked call the outer class
function "jButton1MouseClicked", why the syntax is ok, where can find the
syntax ? In my memory, inner class just can be used by outer class, why it's
ok to use the private function ?

package javaapplication1;

import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}

public void AddAnother() {
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});

pack();
}// </editor-fold>

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
NewJFrame nf;
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
nf = new NewJFrame();
nf.setVisible(true);
// }
// });
nf.AddAnother();

}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration

}

Regards. Ahan
 
S

Sundar

why the syntax is ok,
In my memory, inner class just can be used by outer class, why it's
ok to use the private function ?

Inner class is a member of the outer class, so it can access the
private members of the outer class. Inner class is very similar to a
member method(In terms of accessing private members), like method it
can also access private members. So the syntax is accepted.


I would suggest you to read more about inner classes and understand
it. It will clear many of your doubts.

You may refer the following links for further details:
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html

-Sundar
http://heysundar.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top