Java
  Home arrow Java arrow Page 4 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 - A Closer Look at Argument Passing


    (Page 4 of 12 )

    In general, there are two ways that a computer language can pass an argument to a subroutine. The first way is call-by-value.This method copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument. The second way an argument can be passed is call-by-reference. In this method, a reference to an argument (not the value of the argument) is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine. As you will see, Java uses both approaches, depending upon what is passed.

    In Java, when you pass a simple type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method. For example, consider the following program:

    // Simple types are passed by value.
    class Test {
     
    void meth(int i, int j) {
        i *= 2;
        j /= 2;
     
    }
    }
    class CallByValue {
      public static void main(String args[]) {
        Test ob = new Test();
       
    int a = 15, b = 20;
       
    System.out.println("a and b before call: " +
                          a + " " + b);
       
    ob.meth(a, b);
       
    System.out.println("a and b after call: " +
                          a + " " + b);
      }
    }

    The output from this program is shown here:

    a and b before call: 15 20
    a and b after call: 15 20

    As you can see, the operations that occur inside meth( ) have no effect on the values of a and b used in the call; their values here did not change to 30 and 10.

    When you pass an object to a method, the situation changes dramatically, because objects are passed by reference. Keep in mind that when you create a variable of a class type, you are only creating a reference to an object. Thus, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects are passed to methods by use of call-by-reference. Changes to the object inside the method do affect the object used as an argument. For example, consider the following program:

    // Objects are passed by reference.
    class Test {
      int a, b;
     
    Test(int i, int j) {
        a = i;
        b = j;
     
    }
     
    // pass an object
      void meth(Test o) {
       
    o.a *= 2;
       
    o.b /= 2;
     
    }
    }
    class CallByRef {
      public static void main(String args[]) {
        Test ob = new Test(15, 20);
       
    System.out.println("ob.a and ob.b before call: " +
                           ob.a + " " + ob.b);
       
    ob.meth(ob);
       
    System.out.println("ob.a and ob.b after call: " +
                           ob.a + " " + ob.b);
      }
    }

    This program generates the following output:

    ob.a and ob.b before call: 15 20
    ob.a and ob.b after call: 30 10

    As you can see, in this case, the actions inside meth( ) have affected the object used as an argument.

    As a point of interest, when an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.

    Remember  When a simple type is passed to a method, it is 
                      done by use of call-by-value. Objects are passed  
                      by use of call-by-reference.

    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 2 hosted by Hostway