Total members 11895 |It is currently Sat Dec 21, 2024 4:48 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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());
   }
}
 
 





Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

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 .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

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));
   }
}




Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
Post new topic Reply to topic  [ 4 posts ] 

  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     -  
 Static Import     -  
 Calling the man Command     -  



Topic Tags

PHP Variables
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com