Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Calling a local variable from a static function

Thu Dec 10, 2009 6:17 pm

Calling local variable from inside a static method:

Following code has this problem :
Code:
class Boat {
    int calcDir() {
        int direction = 2;
        return direction;
    }
}

public class Control {
    public static void main(String args[]){

        Boat boat = new Boat();
        boat.calcDir();
        System.out.println(direction);
    }
}
 


I always get the same error:
cannot find symbol
Symbol : variable direction
Location : class Control


In this case you are calling local variable of a method calcDir() inside another class from a static method. The scope the of int direction , is the function calcDir() only.

Code:

class Boat 
{
   int direction;
   int calcDir() {
      return direction + 1;
   }
}

public class Control {
   public static void main(String args[]){
      int direction = 2;
      Boat boat = new Boat();
      System.out.println(boat.calcDir());
   }
}
 
 




Re: What's wrong?

Sat Dec 12, 2009 11:56 pm

the int direction inside the calcDir() method , has a value of 0 . ( which is the default value of int ) . so the 0 + 1 = 0 ;

the int direction inside the main method , it's scope is bounded to the main method only . Not seen inside the class Boat .

Re: What's wrong?

Sun Dec 13, 2009 8:13 pm

So how do I store the calculated value in calcDir() to be able to use it in class Control?

Re: What's wrong?

Sun Dec 27, 2009 1:53 am

Since you working with 2 classes here you need to set a variables value with the calcDir() method

The first way...
Code:
class Boat {
   int calcDir() {
      int direction = 2;
      return direction;
   }
}

public class Control {
   public static void main(String args[]){
      //Declare a variable to hold the return value of calcDir()
      int tempDir = 0;
      Boat boat = new Boat();
   
      //Use the calcDir() method to fill the value of tempDir
      tempDir = boat.calcDir();

      //Print the value
      System.out.println(tempDir);
   }
}


The second way is to pass in a variable amount as a parameter to the calcDir() method...
Code:
class Boat {
   int calcDir(int tVal) {
      //Pass in a value as a parameter to the calcDir() method
      //This Value will get incremented by 1
      return tVal + 1;
   }
}

public class Control {
   public static void main(String args[]){
      int direction = 2;
      Boat boat = new Boat();
      System.out.println(boat.calcDir(direction));
   }
}



Post a reply
  Related Posts  to : Calling a local variable from a static function
 static variable defined in function     -  
 different between Local variable and Global variable     -  
 Calling an Overridden Function     -  
 use local user dll from website - usar dll local del usuario     -  
 difference between a static and a non-static inner class     -  
 Local Web designers     -  
 Need help getting rowcount from local table using javascript     -  
 VB/Oracle Developer-Must be local to Minnesota     -  
 What is a static method     -  
 Static Import     -  

Topic Tags

PHP Variables