Home arrow Java arrow Page 5 - A Closer Look at Methods and Classes
JAVA

A Closer Look at Methods and Classes


Have you ever wanted to get an in-depth understanding of methods and classes in Java? Look no further. 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). 

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 22
June 30, 2005
TABLE OF CONTENTS:
  1. · A Closer Look at Methods and Classes
  2. · Overloading Constructors
  3. · Using Objects as Parameters
  4. · A Closer Look at Argument Passing
  5. · Returning Objects
  6. · Recursion
  7. · Introducing Access Control
  8. · Understanding static
  9. · Introducing final
  10. · Introducing Nested and Inner Classes
  11. · Exploring the String Class
  12. · Using Command-Line Arguments

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials