Java
  Home arrow Java arrow Page 4 - A Fast Gateway to OOP in Java using NetBea...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

A Fast Gateway to OOP in Java using NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-06-26

    Table of Contents:
  • A Fast Gateway to OOP in Java using NetBeans IDE
  • Adding your own class
  • Customizing your own class
  • Accessing the members in “test”
  • Working with more than one object

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Fast Gateway to OOP in Java using NetBeans IDE - Accessing the members in “test”


    (Page 4 of 5 )

    Now, get back to the frame “test.java.”  Double click on it to open, and finally double click on the button to open source view (which automatically generates an event for that button).  Within the source view, modify your “buttonActionPerformed” in such a way that it looks like the following:

    private void btnShowActionPerformed(java.awt.event.ActionEvent
    evt) {
    // TODO add your handling code here:
            MyCalc obj = new MyCalc();
            obj.x = 10;
            obj.y = 20;
            int r = obj.getSum();
            this.lblMsg.setText("Sum = " + String.valueOf(r));
        }

    Once you execute your application (by pressing F5), you will be able to see a window with a button “Show.”  Once you click on it, it shows the message “Sum=30” (Fig08)

    Now, let us go through the code given above.  Let us look at the following statement:

            MyCalc obj = new MyCalc();

    The above statement simply creates an “instance” of the class “MyCalc.”  An “instance” is nothing but some memory area, which gets occupied in RAM to hold all the members of class (along with their values). 

    In this case, the name of the “instance” is “obj.”  We can also call an “instance” as “object.”  An “instance/object” holds all the members of a particular class in memory.  Thus the same “instance/object” (obj) internally contains the members “x,” “y” and “getSum.”  To access any member in an “instance,” we need to first write the name of the instance (obj) and the name of the member (x,y,getSum), separated with a period (dot).

    The same process is being followed in the following:

            obj.x = 10;
            obj.y = 20;

    We are simply assigning some values to the members available in the instance “obj.”  Since “getSum” is a method which returns an “int” value, we need to assign it to the same type of variable.  And thus we have the following:

            int r = obj.getSum();

    And finally, we display the output using the following statement:

            this.lblMsg.setText("Sum = " + String.valueOf(r));

    From here on, I shall stick to using the word “object” rather than “instance” (anyway, they mean the same thing).

    More Java Articles
    More By Jagadish Chaterjee


       · hello guys. I am starting OOP with Java along with NetBeans IDE. enjoy the...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT