Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java - What do parameter lists do?

  1. Oct 11, 2011 #1
    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 (Text):

    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: Oct 11, 2011
  2. jcsd
  3. Oct 11, 2011 #2

    Mark44

    Staff: Mentor

    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.
    There are two variables: collar and _collar. Your code has an error in it in the declaration
    Code (Text):
    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 (Text):
    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.
     
  4. Oct 11, 2011 #3
    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());

    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?

    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?
     
  5. Oct 11, 2011 #4

    Mark44

    Staff: Mentor

    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.
    No.
    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.
    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.
     
  6. Oct 12, 2011 #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 (Text):

    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 (Text):
    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: Oct 13, 2011
  7. Oct 14, 2011 #6
    In your second code something doesn't seem to be right.
    Code (Text):

    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 (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java - What do parameter lists do?
  1. Java linked list (Replies: 1)

Loading...