Java - What do parameter lists do?

  • Java
  • Thread starter prosteve037
  • Start date
  • #1
105
0

Main Question or Discussion Point

In class the other day, my professor had written this as part of the lecture notes:

The parameter list is a (possibly empty) list of parameter declarations. Recall that a parameter is simply a local variable whose initial value is provided by the corresponding argument in the method call. If more than one parameter is declared in a method's parameter list, a comma ',' is used to separate the declarations. The parameter list is delimited by parentheses, '(' and ')'.
Thus, this definition was mentioned as part of methods. He then went on to show an example of this as in the following:
Code:
public class Dog {
  private Collar _ collar;
  public Dog(Collar collar) {
    _collar = collar;
  }
}
Now this is not a method definition, but rather a constructor definition. What I'm trying to understand is what the difference is between the two (if there are any).

Also, to be even more specific, the underlined bold parts are what confuse me. If a parameter of a method is a local variable that initially doesn't have a value until called on, why is the variable declaration in the parameter list needed?

In the given example, we wanted to associate the "Dog" class with the "Collar" class. But I don't understand what's going on with the parameter "Collar _collar". Why is this, along with the "_collar = collar" declaration, necessary?
 
Last edited by a moderator:

Answers and Replies

  • #2
33,719
5,417
In class the other day, my professor had written this as part of the lecture notes:



Thus, this definition was mentioned as part of methods. He then went on to show an example of this as in the following:
Code:
public class Dog {
  private Collar _ collar;
  public Dog(Collar collar) {
    _collar = collar;
  }
}
Now this is not a method definition, but rather a constructor definition. What I'm trying to understand is what the difference is between the two (if there are any).
A constructor is a method, so relative to parameter lists, there is no difference. The most significant difference between a constructor and a class method is that the constructor is declared and defined without a return type.
Also, to be even more specific, the underlined bold parts (Edit: Underlining and bolding removed by moderator to make code easier to read.) are what confuse me. If a parameter of a method is a local variable that initially doesn't have a value until called on, why is the variable declaration in the parameter list needed?

In the given example, we wanted to associate the "Dog" class with the "Collar" class. But I don't understand what's going on with the parameter "Collar _collar". Why is this, along with the "_collar = collar" declaration, necessary?
There are two variables: collar and _collar. Your code has an error in it in the declaration
Code:
private Collar _ collar;
There should not be a space between _ and collar.

The collar variable is a formal parameter of the Dog constructor. The _collar member variable is one of two members of the Dog class, with the other being the constructor. To initialize a Dog instance (or object), you do something like this:
Code:
Dog myDog = new Dog(new Collar(...));
I'm assuming that Collar is a class, so the code above creates a Dog object, initializing it with a Collar instance. That Collar instance gets passed to the code for the Dog constructor, which in turn copies the actual parameter (a Collar instance) to the formal parameter collar. The code for the Dog constructor then assigns the value of the collar parameter to the _collar member variable.

This is a pretty lame example, which makes an explanation more difficult than it really needs to be. Suffice it to say that collar is a variable whose scope is limited to the Dog constructor. _scope is a member variable whose scope is the Dog class.
 
  • #3
105
0
There are two variables: collar and _collar. Your code has an error in it in the declaration
Code:
private Collar _ collar;
There should not be a space between _ and collar.
Oops! Sorry! I meant to type private Collar _collar; :P

To initialize a Dog instance (or object), you do something like this:
Code:
Dog myDog = new Dog(new Collar(...));
I'm assuming that Collar is a class, so the code above creates a Dog object, initializing it with a Collar instance. That Collar instance gets passed to the code for the Dog constructor, which in turn copies the actual parameter (a Collar instance) to the formal parameter collar. The code for the Dog constructor then assigns the value of the collar parameter to the _collar member variable.
So assuming that there was also a "Leash" class, what would happen if I typed Dog myDog = new Dog(new Leash());

Would the argument still get passed to the parameter list of the constructor method (public Dog(Collar collar))?

And how about if I didn't have an argument at all? (Dog myDog = new Dog();) Would the object be instantiated at all?

This is a pretty lame example, which makes an explanation more difficult than it really needs to be. Suffice it to say that collar is a variable whose scope is limited to the Dog constructor. _scope is a member variable whose scope is the Dog class.
Yeah I don't really know why he chose this as an example... And by scope do you mean the "duration of effect" of the variable?
 
  • #4
33,719
5,417
Oops! Sorry! I meant to type private Collar _collar; :P



So assuming that there was also a "Leash" class, what would happen if I typed Dog myDog = new Dog(new Leash());
The type of actual argument in the call to the constructor has to agree with the type of the formal parameter in the definition of the constructor. Since the constructor definition indicates that the parameter is of type Collar, the compiler would complain about the code above.
Would the argument still get passed to the parameter list of the constructor method (public Dog(Collar collar))?
No.
And how about if I didn't have an argument at all? (Dog myDog = new Dog();) Would the object be instantiated at all?
No, based on the definition of the Dog class that you showed in post #1.

I'm reasonably sure of my answers here, despite the fact that I haven't written any Java code since about 1996.
Yeah I don't really know why he chose this as an example... And by scope do you mean the "duration of effect" of the variable?
It's more about the extent to which a variable is known and can be referenced within a program. A variable that is declared inside a method, and this includes parameters in the parameter list, has a scope that is limited to that method. IOW, such a variable can be referenced only within that method. Such variables are called local variables. There is also a sense of time duration, like you mentioned. In C and C++, and probably also in Java, variables that are declared within a method "spring to life" when the method is called, and cease to exist when the method exits.
 
  • #5
105
0
Okay thanks Mark, that cleared things up for me quite a bit.

But now what about the necessity of parameters if you're trying to call a method in a different class?

This example was shown to the class today:

Code:
public class ColourHolder{
private java.awt.colour _colour;
...
public void setColour(java.awt.colour colour){[INDENT]_colour = colour;[/INDENT]}
...
}

Would the parameters (java.awt.colour colour) and _ch.setColour(colour) be necessary in the code below?

Code:
public class ColourButton{
private ColourHolder _ch;
public void pressedButton(java.awt.colour colour){
[INDENT]_colour = colour;[/INDENT]
[INDENT]_ch.setColour(colour);[/INDENT]
}
...
}
The idea was to try and set the color of a separate object (not shown) by implicitly using the setColour method through a different method of a different class (the pressedButton method in the ColourButton class). This is the example that was put up during lecture but I was wondering if this could be written without the explicit parameter colour in the method call _ch.setColour.
 
Last edited:
  • #6
703
13
In your second code something doesn't seem to be right.
Code:
public class ColourButton{
private ColourHolder _ch;
public void pressedButton(java.awt.colour colour){
_colour = colour;
_ch.setColour(colour);
}
...
}
In line 4 you write: _colour = colour;
That doesn't make sense since _colour is not an attribute of the class ColourButton.

Upon reviewing your first code it seems as if your trying to change the attribute _colour of a ColourHolder object.

Code:
public class ColourHolder{
private java.awt.colour _colour;
...
public void setColour(java.awt.colour colour){
_colour = colour;
}
...
}
Since in line 2 the attribute _colour is declared as private you can only change it by using a method, namely the setColour() method.
 

Related Threads on Java - What do parameter lists do?

Replies
9
Views
871
Replies
2
Views
523
  • Last Post
Replies
1
Views
2K
Replies
6
Views
4K
  • Last Post
Replies
1
Views
2K
Replies
3
Views
4K
Replies
6
Views
12K
Replies
20
Views
30K
Replies
3
Views
2K
Top