Java Java - What do parameter lists do?

  • Thread starter Thread starter prosteve037
  • Start date Start date
  • Tags Tags
    Java Parameter
AI Thread Summary
The discussion centers on the role of parameter lists in Java, particularly in the context of method and constructor definitions. A parameter list consists of local variable declarations that receive values from arguments during method calls, and this applies equally to constructors, which differ only in lacking a return type. The example provided illustrates how a constructor initializes a member variable using a parameter, clarifying the distinction between local and member variable scopes. The necessity of parameters is emphasized, as they are essential for passing values to methods, including those in different classes, and cannot be omitted without causing errors. Understanding the scope and purpose of parameters is crucial for effective Java programming.
prosteve037
Messages
110
Reaction score
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
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.
 
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?
 
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.
 
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:
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top