Java - What do parameter lists do?

In summary: Post was too long to include).In summary, a parameter list is a list of parameter declarations and is used in method and constructor definitions. A constructor is a type of method that does not have a return type. The difference between a constructor and a class method is that a constructor is used to initialize objects of a class, while a class method performs a specific task. The parameter list is delimited by parentheses and can contain multiple parameters separated by commas. In the given example, the code shows the use of a constructor to initialize a Dog object with a Collar instance. The variable in the parameter list has a limited scope within the constructor, while the variable in the class declaration has a scope within the entire class. If the argument passed to
  • #1
prosteve037
110
3
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:
Technology news on Phys.org
  • #2
prosteve037 said:
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.
prosteve037 said:
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
Mark44 said:
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

Mark44 said:
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?

Mark44 said:
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
prosteve037 said:
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.
prosteve037 said:
Would the argument still get passed to the parameter list of the constructor method (public Dog(Collar collar))?
No.
prosteve037 said:
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.
prosteve037 said:
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
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
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 to Java - What do parameter lists do?

1. What is the purpose of a parameter list in Java?

A parameter list in Java allows the programmer to pass data or variables into a method when it is called. This allows the method to use and manipulate the data passed in, making the code more dynamic and reusable.

2. How do you declare a parameter list in Java?

To declare a parameter list in Java, you must include the data type and name of each parameter within parentheses following the method name. For example: public void myMethod(int num1, int num2) { // code goes here }

3. Can a method have multiple parameter lists in Java?

No, a method can only have one parameter list in Java. However, the parameter list can include multiple parameters separated by commas.

4. Can the order of parameters in a parameter list be changed in Java?

Yes, the order of parameters in a parameter list can be changed in Java. However, the data type and number of parameters must remain the same for the method to work correctly.

5. Are parameter lists required in Java methods?

No, parameter lists are not required in Java methods. If a method does not need any data or variables to be passed into it, then the method can be declared without a parameter list. For example: public void myMethod() { // code goes here }

Similar threads

  • Programming and Computer Science
Replies
3
Views
804
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
13
Views
898
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
458
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top