• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method relationship

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks, I am challenged with understanding arg:

Specifically, how does the arg variable come into play? (I desire to know how the 42 comes into contact with being multiplied by 2.)
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XCopy x = new XCopy();

This line makes an object of the XCopy class and assigns the variable x to reference it. This object is able to access all of the methods in the XCopy class, namely the go() method.

int y = x.go(orig);

This line uses the object which x references to access its go() method. The go method requires the program to pass in an int, which is orig in this case. The go method receives the value orig, which will be called arg within the method. arg is multiplied by 2 and then returned. This returned value is the value that y is set equal to.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you taken aglebra in school? Remember when you learned about functions? Such as f(x) = x + 5, so that f(1) = 6, f(2) = 7, etc.?

Well, the definition of the function, f(x) = x + 5, is kind of like the definition of a Java method:



So in your case, when we call the method go(), it takes its argument, multiplies it by 2, and returns the result. It's like we defined a function go(x) = x * 2 and then we "call the function" (invoke the method) on line 6 of your code:
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When calling a method in Java that expects parameters, you always pass the arguments inside the braces as they are defined in the method signature while calling the method. Here , you have the go(int) method that receives the integer 42, multiplies it my 2 and returns the argument back to the caller. What part is it that you are confused about? Read this for a clear understanding of method declaration.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:
This line makes an object of the XCopy class called x.
...
This line uses the object x to access its go() method



Two small nitpicks, for the benefit of the OP:

  • Objects don't have names. It's the variable that's caleld x. The object isn't called anything.
  • It's not "the object x", but rather "the object pointed to by the reference value in variable x."


  • Often times in casual discussion we'll ignore the distinction between objects and the variables that point to them, for the sake of brevity. Beginners are often unaware of that distinction, however, so it's usually a good idea to make it explicit in discussions like this.
     
    Joel Christophel
    Ranch Hand
    Posts: 250
    1
    Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeff Verdegan wrote:

    Joel Christophel wrote:
    This line makes an object of the XCopy class called x.
    ...
    This line uses the object x to access its go() method



    Two small nitpicks, for the benefit of the OP:

  • Objects don't have names. It's the variable that's caleld x. The object isn't called anything.
  • It's not "the object x", but rather "the object pointed to by the reference value in variable x."


  • Often times in casual discussion we'll ignore the distinction between objects and the variables that point to them, for the sake of brevity. Beginners are often unaware of that distinction, however, so it's usually a good idea to make it explicit in discussions like this.



    If you check back you'll see that it's already been changed. I assumed someone would bring that up...
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeff Verdegan wrote:So in your case, when we call the method go(), it takes its argument, multiplies it by 2, and returns the result. It's like we defined a function go(x) = x * 2 and then we "call the function" (invoke the method) on line 6 of your code:



    And in case it wasn't clear, the reason we're multiplying 42 by 2 is because before calling go(orig); we did:

    so the variable orig had the value 42.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joel Christophel wrote:If you check back you'll see that it's already been changed. I assumed someone would bring that up...



    Well done, SeƱor Queeksdraw!
     
    WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic