4 Replies Latest reply on May 17, 2009 11:58 PM by 843789

    Declaring an object reference

    843789
      i am a beginner in Java and i am studying methods. My professor gave me a sample code and one of the line which i fail to understand is as follows
      public double addShirt(Shirt s)
      I am having problem in understanding what the parenthesis (Shirt s) does.
      Now i believe this is creating a new object reference "s" for class Shirt but it is missing the keyword "new" and from my previous experience i have used something like
      Shirt s = new Shirt()
      but here i dont see the "new" keyword. Please explain what is this?


      really appreciate your help.

      Edited by: pipsonian on May 17, 2009 3:23 PM
        • 1. Re: Declaring an object reference
          843789
          pipsonian wrote:
          public double addShirt(Shirt s)
          This is the first line of a new method, addShirt. Just underneighth this, you should find the body of the method that is enclosed within curly braces - { }. The Shirt variable, s, is the parameter of the method and tells you that whenever you call this method, you must pass a Shirt variable inside of the parenthesis. The method body will then use this parameter.

          For instance this method takes a String parameter and prints it:
          public void myPrint(String str)
          {
            System.out.println(str);  // here I use the str parameter and print it.
          }
          I would call this method like so:
          String fooString = "hello world";
          myPrint(fooString);
          • 2. Re: Declaring an object reference
            843789
            thanks a lot. now i realize it was a very dumb question but somehow never came to my mind.

            really appreciate your help.
            • 3. Re: Declaring an object reference
              843789
              No prob. The Sun tutorial and your textbook should give you more information on this. You can find help here:
              [http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html]
              [http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html]
              • 4. Re: Declaring an object reference
                843789
                pipsonian wrote:
                thanks a lot. now i realize it was a very dumb question but somehow never came to my mind.
                Don't feel so bad. I remember when I was doing first year at uni and a lot of people didn't understand parameters. So much that the lecturer put on a special tutorial session to explain them in more detail than he could in a normal lecture. Mind you we used Ada in my first year and the problems were compounded by the fact that you could have in parameters, out parameters and in/out parameters.