Java
  Home arrow Java arrow Page 5 - A Closer Look at Methods and Classes
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 Closer Look at Methods and Classes
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2005-06-30

    Table of Contents:
  • A Closer Look at Methods and Classes
  • Overloading Constructors
  • Using Objects as Parameters
  • A Closer Look at Argument Passing
  • Returning Objects
  • Recursion
  • Introducing Access Control
  • Understanding static
  • Introducing final
  • Introducing Nested and Inner Classes
  • Exploring the String Class
  • Using Command-Line Arguments

  • 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 Closer Look at Methods and Classes - Returning Objects


    (Page 5 of 12 )

    A method can return any type of data, including class types that you create. For example, in the following program, the incrByTen( ) method returns an object in which the value of a is ten greater than it is in the invoking object.

    // Returning an object.
    class Test {
      int a;
     
    Test(int i) {
        a = i;
      }
     
    Test incrByTen() {
        Test temp = new Test(a+10);
        return temp;
     
    }
    }
    class RetOb {
     
    public static void main(String args[]) {
        Test ob1 = new Test(2);
        Test ob2;
       
    ob2 = ob1.incrByTen();
        System.out.println("ob1.a: " + ob1.a); 
        System.out.println("ob2.a: " + ob2.a);
       
    ob2 = ob2.incrByTen();
        System.out.println("ob2.a after second increase: "
                            + ob2.a);
      }
    }

    The output generated by this program is shown here:

    ob1.a: 2
    ob2.a: 12
    ob2.a after second increase: 22

    As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference to it is returned to the calling routine.

    The preceding program makes another important point: Since all objects are dynamically allocated using new, you don’t need to worry about an object going out-of-scope because the method in which it was created terminates. The object will continue to exist as long as there is a reference to it somewhere in your program. When there are no references to it, the object will be reclaimed the next time garbage collection takes place.

    More Java Articles
    More By McGraw-Hill/Osborne


     

    Buy this book now. This article is excerpted from chapter 7 of Java: the Complete Reference, J2SE 5 Edition, written by Herbert Schildt (McGraw-Hill/Osborne, 2004; ISBN: 0072230738). Check it out at your favorite bookstore. Buy this book now.

    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