• Post Reply Bookmark Topic Watch Topic
  • New Topic

method relationship  RSS feed

 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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.)
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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:
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • 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
    • 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
    • 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!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!