Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default how to use a variable of one method in another method

    Hello! I'm an engineering student, still beggining my JAVA studies...
    i've got the flollowing doubt:

    when i want to use a method (met2) inside another method (met1), i must use class.met2(variable), right?

    what happens is:
    in the method met2, i've got a loop (recursion), in which the value of the variable "a" varies in each recursive cycle.
    i wanna use the current value of "a" in the method met1....

    so I tried to put:

    int u= a.met2

    or

    int u=met2.a

    and i doesn't work..

    if i just use "a" in the method met1, the compiler - eclipse - claims, naturally: "a cannot be solved"...

    will anyone gimme some help?

    thanks a lot!

    Lucas.
    _________________

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,546
    Rep Power
    23

    Default

    i(t) doesn't work..
    What doesn't work? Do you get an error message? Post it here with the code.

    If the variable a is local to a method, then you can not access it from another method. a needs to be a class variable to be accessible to other methods and classes.
    You can pass the value of a to another method by using it as a parameter:

    public void method1() {
    int a = ...;
    ...
    method2(a); // pass a to method2
    } // end method1()

  3. #3
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    public static int sumDivr (int x, int xInicial) {
    if (x==0)
    return 0;

    else if (xInicial%x==0 && x!=xInicial)
    return x + sumDivr (x-1,xInicial);

    else
    return 0 + sumDivr (x-1,xInicial);

    }

    public static int sumDoisAmig (int f, int g) {
    return (sumDivr(f,f)==g && sumDivr(g,g)==f && f!=g) ? (f+g) : 0 ;
    }


    public static int sumAmigosr (int n, int b) {

    int u=a.sumFinalr; /* i want the variable "u" to have the value that the variable "a", of the method "sumFinalr", has at the current instant*/

    if (b==n) return 0;

    else return sumDoisAmig (u,b) + sumAmigosr (n,b+1);

    }

    public static int sumFinalr (int n, int a) {

    if (a==n) return 0;

    else return sumAmigosr(n,a) + sumFinalr(n,a+1) ;

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,546
    Rep Power
    23

    Default

    the value that the variable "a", of the method "sumFinalr" has at the current instant
    The only way would be for the method sumFinalr to use that value in its call to another method. In sumFinalr it could pass the value of a to another method as a parameter:
    sumAmigosr(a,...);// pass the value of a to sumAmigosr

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  3. Get method
    By Reiyn in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 06:12 PM
  4. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  5. Renaming a method/variable
    By gapper in forum Eclipse
    Replies: 0
    Last Post: 01-31-2008, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •