Java
  Home arrow Java arrow Page 3 - Accepting and Returning Objects when Progr...
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

Accepting and Returning Objects when Programming with Methods in Java
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-06-27

    Table of Contents:
  • Accepting and Returning Objects when Programming with Methods in Java
  • Methods accepting objects as parameters: demo
  • Methods accepting objects as parameters: explanation
  • Methods returning objects: demo
  • Methods returning objects: explanation

  • 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


    Accepting and Returning Objects when Programming with Methods in Java - Methods accepting objects as parameters: explanation


    (Page 3 of 5 )

    This section will explain the coding listed in the previous section.  Let us start with the following:

        int x=0;
        int y=0;

    You can observe that I declared three fields/attributes for the class “MyCalc.” And the following is the new “method” I introduced:

        public void copyValues(MyCalc c) {
            x = c.x;
            y = c.y;
        }

    The above method is named “copyValues” and it returns the “void” type.  The “void” indeed is simply “nothing.” In other words, the method “calcSum” doesn’t return any value after its execution.   

    The most important issue is the parameter we are required to pass to it.  The parameter which it accepts must be of type “MyCalc.”  That means I need to create an object of type “MyCalc” (in the calling program), fill it with values and finally call this method (“copyValues”) by passing the same object.  The object which is passed from the calling program will be referred to as “c.” 

    You can observe that “c” is of type “MyCalc.”  That means it is an object which is equipped with all the members of the class “MyCalc.” Once the object is received from the calling program, the runtime executes the following statements:

            x = c.x;
            y = c.y;

    The above statements simply assign the values available in “c” to the current object which is executing this method.

    Now, if we proceed to our “test” frame, we have the following initially:

            MyCalc obj1 = new MyCalc();
            MyCalc obj2 = new MyCalc();

            obj1.x = 10;
            obj1.y = 20;

    I declared two objects named “obj1” and “obj2.” Now, both of them internally contain members (x, y, copyValues, getSum).  You can also observe that I am simply providing the values of “x” and “y” for only “obj1.” Proceeding further, we have the following:

            obj2.copyValues(obj1); 

    The above is the most important statement of our example.  I am calling the method “copyValues” from “obj2” (where x and y in that object are not assigned with any values yet), by passing “obj1.”  The object which is being passed (“obj1”) will be referred to as “c” by the method “copyValues.” During the execution of the method “copyValues,” the values of “x” and “y” belonging to “c” will be assigned to “x” and “y” of “obj2” as the “copyValues” is being executed from “obj2.” Proceeding further, we have the following:

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

            r = obj2.getSum();
            this.lblMsg2.setText("Sum = " + String.valueOf(r));

    The above code dues nothing but get the sum of “x” and “y” and display it. My previous article gives you a better understanding of this.

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys. another contribution on OOPS with java and NetBeans IDE.
     

    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 5 hosted by Hostway
    Stay green...Green IT