7 Replies Latest reply: Jan 8, 2013 2:00 AM by Kayaman RSS

    A question on methods and parameters.

    983190
      Hey guys, it's my first time posting here. I'm very new to Java, and did a bit of C++ prior to Java. I had a question on methods and parameters. I don't quite understand methods; I know that they can be repeated when called, but thats almost about it. I also know that a program must have one class that holds the main method. What I truly, truly don't understand about methods is what parameters are. I know they go in the parentheses, and that's it. Could you please explain what they are? I truly appreciate it. Thanks to all in advance. Regards, Michael
        • 1. Re: A question on methods and parameters.
          983190
          Any help, please...?
          • 2. Re: A question on methods and parameters.
            rp0428
            You should work your way through the Java Tutorial. This is the section on methods and it covers parameters.

            http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
            • 3. Re: A question on methods and parameters.
              gimbal2
              980187 wrote:
              I'm very new to Java, and did a bit of C++
              How is that possible when you don't even know what parameters are?

              Anyway perhaps you should consider buying a decent book to learn the language, that at least you CAN learn from a book. The other option is that you ask a million "how does this work" questions in a forum, which I will assure you is going to net you quite unfriendly responses eventually when the patience of people runs out. Lack of knowledge is not a problem because it is easily solved: get decent study material.
              • 4. Re: A question on methods and parameters.
                983190
                Hi, I was looking at the book, and many tutorials, and no where there did I understand them. That is why I came here to ask the question. I will not be asking newbie questions it's just that this specific thing I didn't understand. I understand what a method is, but not really parameters, and whether methods can be made inside of the main method? Thanks, guys.
                • 5. Re: A question on methods and parameters.
                  983323
                  Taking an example :
                  Suppose you are calculating area of rectangle you need two inputs one is length and breadth.Area = l X b where l = length, b = breadth

                  So your method, say, calculateAreaOfRectangle(int length, int breadth) will have two input parameters as arguments.

                  System.out.println("Area of rectangle:"+calculateAreaOfRectangle(40,30);


                  public int calculateAreaOfRectangle(int length, int breadth) {
                  int Area;
                  Area = length * breadth;
                  return Area;
                  }

                  So if you call this method then the output will be returned as 120.



                  Parameters of a method are just the input variables needed for the method to process for any calculations or anything useful.

                  And we cant have methods inside main method in Java. It is against the java syntax and if you do, it will throw a syntax error.
                  • 6. Re: A question on methods and parameters.
                    983190
                    Thank you so, so much for that helpful explanation with the length x width (breadth). Thank you! It was GREAT. I now get it.

                    Edited by: MW130 on Jan 8, 2013 5:41 PM
                    • 7. Re: A question on methods and parameters.
                      Kayaman
                      MW130 wrote:
                      I know get it.
                      "Know you don't."

                      Read the tutorial, that's an order.