7.3 Method parametersHomepage  « Java6 Certification « 7.3 Method parameters

In this lesson we look at method parameters that are passed into methods as object references and primitive values and the effects that modification within the methods has on the type in question.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 7: Fundamentals

    • Determine the effect upon object references and primitive values when they are passed into methods that perform assignments or other modifying operations on the parameters.

Pass-By-Valuego to top of page Top

When we pass an argument to a method we are actually passing a copy of that argument and this is known as pass-by-value. To explore what this means lets look at some code:



/*
  A Dog Class
*/ 
public class Dog {
    String name, colour;

    void dogLooks(String name, String colour) {
        System.out.println("Our dog " + name + " is " + colour + "(input arguments)");  
        name = "zzz";        
        colour = "zzz";        
        System.out.println("Our dog " + name + " is " + colour + "(changed arguments)");  
    }
}

Lets write a test class for our Dog class.



/*
  Test Class for Dog
*/ 
public class TestDog {

    public static void main (String[] args) {
        Dog doggy = new Dog();  // Create a Dog instance
        String name = "rover"; 
        String colour = "black"; 
        doggy.dogLooks(name, colour);
        System.out.println("Our dog " + name + " is " + colour + "(after call to dogLooks() )");  
    }
}

run test dog

The above screenshot shows the output of running our TestDog class. As you can see even though we change the parameters in the dogLooks method, when we return and print the passed arguments they are the same as before the call to dogLooks. This is because Java made a copy of the variables and passed them into our method. Therefore any changes to the variables were made to the copies and not to the originating argument list.



Pass-By-Value Using Reference Variables

So what if we pass a reference variable to a method? If you recall our objects live on The Heap and we access them through their reference variable. Lets look into this:



/*
  Another Dog Class
*/ 
public class Dog2 {
    String name, colour;

    void dogLooks(Dog2 dog) {
        System.out.println("Our dog " + dog.name + " is " + dog.colour + "(input arguments)");  
        dog.name = "zzz";        
        dog.colour = "zzz";        
        System.out.println("Our dog " + dog.name + " is " + dog.colour + "(changed arguments)");  
    }
}

Lets write a test class for our Dog2 class.



/*
  Test Class for Dog2
*/ 
public class TestDog2 {

    public static void main (String[] args) {
        Dog2 doggy = new Dog2();  // Create a Dog instance
        doggy.name = "rover"; 
        doggy.colour = "black"; 
        doggy.dogLooks(doggy);  // Pass a reference variable to a method
        System.out.println("Our dog " + doggy.name + " is " + doggy.colour 
                                      + "(after call to dogLooks() )");  
    }
}

run test dog2

The above screenshot shows the output of running our TestDog2 class. As you can see when we change the instance variables in the method the values have changed when we return. It is still passing a copy of the reference variable to the method, but we are updating the actual instance variables on The Heap. So the changes in the method persist when the dogLooks method ends.



Related Java6 Tutorials

Beginning Java6 - Primitive Variables
Objects & Classes - Reference Variables
Objects & Classes - Methods


go to home page Java6 Tutor Homepage go to home page Top






All the Java6 Certification Fundamentals lessons are listed below. Click a link to go to that lesson.

7: Fundamentals

7.1-7.2-7.5 javac/java & JAR files

7.3 Method parameters

Pass By Value

7.4 Garbage Collection

7.6 Java Operators