-1

I'm new to Java and wanted some clarification, I understand that I'm declaring an int variable of x inside the method's parameters but why is it that 'result' can not be resolved to a variable.

public class Methods{

    public static void main(String[] args) {

        //f(x) = x * x
        square(5);
        System.out.println(result);

    }

    //This method 
    static int square(int x) {
        int result =  x * x;
    }
user207421
  • 305,947
  • 44
  • 307
  • 483
user3130676
  • 1
  • 1
  • 5
  • Like you said, it's a **local variable**, so it's not going to be defined in `main()`. You could return `result` that and store it in a variable in `main()` instead. – Spencer Wieczorek Mar 20 '15 at 05:35

2 Answers2

1

You can, but note that local variable are only defined in their respected functions. So even though result is defined in square(), it's not defined in main(). So what you want to do is return a value for your square function and store that in a variable inside main() like so:

public static void main(String[] args) {

    int myResult = square(5);
    System.out.println(myResult);
}

//This method 
static int square(int x) {
    int result =  x * x; // Note we could just say: return x * x;
    return result; 
}

Example Here

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • 1
    OHHHH! okay. So this means that no other local variable except for returning values can be executed in main and will only work withing it's own method ONLY to return? – user3130676 Mar 20 '15 at 05:54
  • @user3130676 Yes. Returning gives back a value, so the function call `square(5)` would be as if it's the number `25`. Local variables are only defined in the functions they where made in, they are gone after the function is done. But you could give back the value of them. – Spencer Wieczorek Mar 20 '15 at 05:58
  • Can values returned be Strings as well? and how other way can I return the value besides using the System.out.println(square()) in the main method? – user3130676 Mar 20 '15 at 06:07
  • @user3130676 Just calling the function `square()` returns the value. You can use strings but you need to know that `int square` means it expects the returned value to be an integer. If you want it to be a string change it to `String square`. – Spencer Wieczorek Mar 20 '15 at 06:10
  • Right, so it would be square(5) (I need to input an int so it could return a value)? – user3130676 Mar 20 '15 at 06:22
  • @user3130676 In Java when you do `int functionName( ... )` the `int` part means that it expects that function to return a value and that value must be an integer. It doesn't matter what everything else is (the parameters could be whatever you want), just that when you do `return` it needs to be an integer. – Spencer Wieczorek Mar 20 '15 at 06:25
  • Thank you for clarifying this. May I ask, how did you learn Java? And if there are some tips and resources that can be helpful to really understand everything (or at least enough to feel proficient writing in Java) about Java? – user3130676 Mar 20 '15 at 06:31
  • @user3130676 Oracle has some [good tutorials](http://docs.oracle.com/javase/tutorial/). The best way to learn is to do and practice. As a programmer it's important not to think in a certain language, (I'm a JavaScript guy, I don't use Java often), learn the concepts and know how to solve a problem logically or with pseudo code. Don't worry too much about knowing syntax, basically anything you can easily look up isn't worth memorizing. More importantly learn how to look things up (this is where knowing general concepts comes in handy). That way you can use any language even if you don't know it – Spencer Wieczorek Mar 20 '15 at 06:41
0

Since you are beginner, I will explain it bit thoroughly

Rule 1:Local variables are declared in methods, constructors, or blocks.

Rule 2:Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.

Rule 3:There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.

public class Methods{

      public static void main(String[] args) {
            //f(x) = x * x
            square(5);
            System.out.println(result); //result! who are you? 
                  //Main will not recognize him because of rule 3.
           }


        static int square(int x) {
            int result =  x * x;  //you did it as said in rule 1
        }//variable result is destroyed because of rule 2.

Please go thorough comments in code.

Well solution for your code is:

public class Methods{

  public static void main(String[] args) {
        //f(x) = x * x
        int result=square(5);
        System.out.println(result); //result! who are you? 
              //Main will not recognize him because of rule 3.
       }
    static int square(int x) {
        int result1 =  x * x;  //you did it as said in rule 1
        return result1;  
    }
VedantK
  • 9,728
  • 7
  • 66
  • 71
  • 2
    I can't use the variable 'result' because it only exists within the method? – user3130676 Mar 20 '15 at 06:50
  • Rule number 2 is wrong in my point of view. It won't be created if a certain block is entered, it will be created if the program execution reaches the declaration. The other part is correct. – Tom Mar 20 '15 at 07:04
  • @Tom to be specific,Yes. please feel free to edit and correct if you are confirm of some things. Thank you – VedantK Mar 20 '15 at 07:13