Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

why inner class can use outer class function ?

Thread view: 
Ahan Hsieh - 05 Oct 2007 07:24 GMT
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
Sundar - 05 Oct 2007 08:11 GMT
>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
Ahan Hsieh - 05 Oct 2007 10:03 GMT
Hi, Sundar :
   Thanks your help, I get more understanding.

Regards. Ahan

> >why the syntax is ok,
> > In my memory, inner class just can be used by outer class, why it's
[quoted text clipped - 14 lines]
> -Sundar
> http://heysundar.blogspot.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.