Java
  Home arrow Java arrow Page 5 - 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 returning objects: explanation


    (Page 5 of 5 )

    This section will explain the code listed in previous section. The following is the new “method” I introduced in the class “MyCalc”:

        public MyCalc getDoubledValues() {
            MyCalc t = new MyCalc();
            t.x = x * x;
            t.y = y * y;
            return t;
        }

    The above is a bit different from any of the methods listed till now.  Let us consider the first line:

        public MyCalc getDoubledValues()

    From the above line, the name of the method is “getDoubledValues." It accepts no parameters and returns a value of type “MyCalc.”  In fact, “MyCalc” is not a standard data type like “int” or “float.”  It is of type “class.” That means the method is trying to return an “object” of type “MyCalc” class. We have the second line as follows:

            MyCalc t = new MyCalc();

    In the above statement, we are simply creating a new temporary object “t,” which is also of type “MyCalc.”  That means again “t” consists of all members available in the “MyCalc” class.  Now, I assign the values to “x” and “y” of “t” based on the double values available in the calling object:

            t.x = x * x;
            t.y = y * y;

    Once the values are filled, we return the temporary object “t” back to the calling program using the following statement:

            return t;

    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;

            obj2 = obj1.getDoubledValues();

    I declared two objects named “obj1” and “obj2.” Now, both of them internally contain members (x, y, copyValues, getDoubledValues, 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 = obj1.getDoubledValues();

    The above is the most important statement of our example.  I am calling the method “getDoubledValues” from “obj1” (which has some values of x and y in it), by assigning the returned object to “obj2.” 

    Once the method is called, the object “t” which is instantiated at “getDoubledValues” will be assigned back to “obj2.”  That means all the members/values in “t” will referred by “obj2” now.

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

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

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

    The entire code for this article is freely available in the form of a zip file. That downloadable solution was developed using NetBeans 4.1 IDE together with Microsoft Windows 2003 Standard Edition. I didn’t really test it in any other version or platform. Please follow the respective platform documentation to get it working. And please note that this series is dedicated to the beginners who wanted to work with NetBeans IDE.

    Any doubts, bugs, errors, suggestions, feedback etc. are highly appreciated at jag_chat@yahoo.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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