-2
class Method {

 public static void main(String[] args) {
    int a =1, b=2;
    method1(a,b);
    method2();
 }

 public static int method1(int a, int b) {
    int c = (a + b);
    return c;
 }

 public static void method2() {
   int z=11;
   if (z >= method1(a,b)) {System.out.println("Method 2 works");}
 }   
}

-getting enter image description hereerror in if statement

4
  • In method2() u r trying to use parameters a,b but they are not in that method. Commented Dec 11, 2016 at 11:40
  • 1
    a and b are method arguments, not result. They are undefined in method2. What are you trying to sum in method2? What z should be greater than? Commented Dec 11, 2016 at 11:40
  • You have to pass a and b argument to method2 in order to be consumed by method 1 Commented Dec 11, 2016 at 11:42
  • 1
    Possible duplicate of Java: "error: cannot find symbol" Commented Dec 11, 2016 at 11:48

5 Answers 5

0

You have not declared variables a and b inside method2, which is causing the error you are seeing.

If you want to use local variables wihthin methods you can try the following:

public static void method2() {
    int a = 1;
    int b = 2;
    int z=11;
    if (z >= method1(a,b)) {System.out.println("Method 2 works");
}   
Sign up to request clarification or add additional context in comments.

Comments

0

If method 2 needs resulting input from another methods then you can do in several ways, is more just like a design style

You can do:

public static void method2(int x) {
   int z=11;
   if (z >= x) {System.out.println("Method 2 works");}
 }

And call it by doing

method2(method1(a,b));

Comments

0

a,b variables are not global.So those variables are local to those methods in which they are defined.

In method 2 you have not defined a,b variables and tried to access them.

For this pass the variables to method2 also as done for method1 or store the method1 result in a variable and pass that variable to method2 and then compare.

Comments

-1

Thank you all for your help. Finaly I got it. I am trying to learn different ways of applying methods. Now it works fine.

class Method {

public static void main(String[] args) {
    int a = 1, b = 2, e = 3;
    System.out.println(method1(a, b));
    System.out.println(method2(a, b, e));
    method3();
}

public static int method1(int a, int b) {

    int c = (a + b);
    System.out.print("Method 1 burada işləyir ");
    return c;
}

public static int method2(int a, int b, int e) {
    int q = (a + b + e);

    System.out.print("Method 2 işləyir və nəticə ");

    return q;
}

public static void method3() {
    int z = 11;
    int a = 4;
    int b = 6;
    if (z >= method1(a, b)) {
        System.out.println("Method 3 işləyir");
    }
}

}

Comments

-1

package com.ZJAVAKITABI;

class Method {

public static void main(String[] args) {
    int a = 1, b = 2, e = 3;
    System.out.println(method1(a, b, e));
    System.out.println(method2());
    method3();
    method4();
    System.out.println(method5(a, b));
}

public static int method1(int a, int b, int e) {
    int q = (a + b + e);
    System.out.print("3 parametrli Method 1 işləyir və nəticə ");
    return q;
}

public static int method1(int a, int b) {
    int mf = (a + b);
    System.out.print("2 parametrli Method1 işləyir. Nəticə: ");
    return mf;
}

public static int method2() {
    int a = 4, b = 6;
    int z = a + b;
    System.out.print("Method 2 işləyir və nəticə ");
    return z;
}

public static void method3() {
    System.out.println("Method 3 işləyir, Void nəticə vermir");
}

public static void method4() {
    int v = 5, m = 5;
    int n = v + m;
    System.out.println("Method 4 işləyir, hesablama nəticəsi " + n);
}

public static int method5(int a, int b) {
    int u = 9;
    System.out.print("Method 5 və daxilindəki ");
    int s = method1(a, b) + u + 2; 
    return s;
}

}

2 Comments

It is an answer? Or some edit to the question? Some explanation would be nice!
this is the final answer what I wanted to test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.