Java
  Home arrow Java arrow Page 3 - 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 
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 - Using Objects as Parameters


    (Page 3 of 12 )

    So far we have only been using simple types as parameters to methods. However, it is both correct and common to pass objects to methods. For example, consider the following short program:

    // Objects may be passed to methods.
    class Test {
      int a, b;
     
    Test(int i, int j) {
        a = i;
        b = j;
     
    }
     
    // return true if o is equal to the invoking object
     
    boolean equals(Test o) {
        if(o.a == a && o.b == b) return true;
        else return false;
     
    }
    }
    class PassOb {
      public static void main(String args[]) {
        
    Test ob1 = new Test(100, 22);
        Test ob2 = new Test(100, 22);
        Test ob3 = new Test(-1, -1);
        
    System.out.println("ob1 == ob2: " + ob1.equals(ob2));
        
    System.out.println("ob1 == ob3: " + ob1.equals(ob3));
      }
    }

    This program generates the following output:

    ob1 == ob2: true
    ob1 == ob3: false

    As you can see, the equals( ) method inside Test compares two objects for equality and returns the result. That is, it compares the invoking object with the one that it is passed. If they contain the same values, then the method returns true. Otherwise, it returns false. Notice that the parameter o in equals( ) specifies Test as its type. Although Test is a class type created by the program, it is used in just the same way as Java’s built-in types.

    One of the most common uses of object parameters involves constructors. Frequently you will want to construct a new object so that it is initially the same as some existing object. To do this, you must define a constructor that takes an object of its class as a parameter. For example, the following version of Box allows one object to initialize another:

    // Here, Box allows one object to initialize another.
    class Box {
      double width;
      double height;
      double depth;
     
    // construct clone of an object
      Box(Box ob) { // pass object to constructor
        width = ob.width;
        height = ob.height;
        depth = ob.depth;
     
    }
      // constructor used when all dimensions specified
     
    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
     
    }
     
    // constructor used when no dimensions specified
     
    Box() {
        width = -1;  // use -1 to indicate
        height = -1; // an uninitialized
        depth = -1;  // box
     
    }
     
    // constructor used when cube is created
      Box(double len)
    {
        width = height = depth = len;
      }
     
    // compute and return volume
      double volume() {
        return width * height * depth;
      }
    }
    class OverloadCons2 {
     
    public static void main(String args[]) {
        // create boxes using the various constructors
        Box mybox1 = new Box(10, 20, 15);
        Box mybox2 = new Box();
        Box mycube = new Box(7);
       
    Box myclone = new Box(mybox1);
       
    double vol;
       
    // get volume of first box
        vol = mybox1.volume();
        System.out.println("Volume of mybox1 is " + vol);
        // get volume of second box
        vol = mybox2.volume();
        System.out.println("Volume of mybox2 is " + vol);
       
    // get volume of cube
        vol = mycube.volume();
        System.out.println("Volume of cube is " + vol);
       
    // get volume of clone
        vol = myclone.volume();
        System.out.println("Volume of clone is " + vol);
     
    }
    }

    As you will see when you begin to create your own classes, providing many forms of constructor methods is usually required to allow objects to be constructed in a convenient and efficient manner.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek