Can I use local variables from one method in another method?

In summary, without adding new Fields, you can use local variables from another method in the same class by wrapping the code in [code] and [/code] tags. The variables must already have values assigned to them.
  • #1
wyosteve
23
0
Without adding new Fields, how do I use local variables from another method in the same class? I know I could simply add these variables to the list of Fields and get to them that way but our instructor specifically told us NOT to do this. I am stumped. Heres the realavent code, in bold are the local variables I want to use again.

public static void main(String[] args)
{
String userName; // the name of the person using this program
String password; // user's password
String input; // temporary String for holding returned values from input dialog boxes
boolean done = false; // false until the employee is done entering products sold

String itemName; // the name of the item sold
String itemID; // product ID # for the item sold
double price; // price for the item
int quantity; // quantity sold

ArrayList<Item> items = new ArrayList<Item>();// holds all items sold by this employee today

code

/**
* Prints out the sales report in an organized format.
*/
public void printReport()
{
int totalPrice = 0;
for(Item item: items)
{
totalPrice = item.getPrice();
}
}

/**
* Verify a user account and password.
*/
public void verifyUser()
{
for(String name: EMPLOYEE)
{
for(String pswd: EMPLOYEE_PSWD)
{
if((userName.equals(name)) && (password.equals(pswd)))
{
do this }
}
}
}
}
Any clue would be appriciated! Oh and first post!
 
Physics news on Phys.org
  • #2
Welcome to PF!

When you include code, wrap it with [ code] and [ /code] tags (without extra spaces) to preserve your formatting. I did that below.
wyosteve said:
Without adding new Fields, how do I use local variables from another method in the same class? I know I could simply add these variables to the list of Fields and get to them that way but our instructor specifically told us NOT to do this. I am stumped. Heres the realavent code, in bold are the local variables I want to use again.
Code:
public static void main(String[] args)
    {
        String [B]userName[/B];        // the name of the person using this program
        String [B]password[/B];        // user's password
        String input;           // temporary String for holding returned values from input dialog boxes
        boolean done = false;   // false until the employee is done entering products sold
        
        String itemName;    // the name of the item sold
        String itemID;      // product ID # for the item sold
        double price;       // price for the item
        int quantity;       // quantity sold
        
        ArrayList<Item>[B] items [/B]= new ArrayList<Item>();// holds all items sold by this employee today

        code

/**
* Prints out the sales report in an organized format.
*/
    public void printReport()
      {
          int totalPrice = 0;
          for(Item item: [B]items[/B])
          {
              totalPrice = item.getPrice();
          }
       }
      
/**
* Verify a user account and password.
*/
    public void verifyUser()
    {
        for(String name: EMPLOYEE)
        {
            for(String pswd: EMPLOYEE_PSWD)
            {
                if(([B]userName[/B].equals(name)) && ([B]password[/B].equals(pswd)))
                {
                   do this                }
                }
            }
        }
    }
Any clue would be appriciated! Oh and first post!

Your two functions should have parameters. That's how you can pass the value of a local variable in main to your functions. However, the variables you pass should already have values. In your code, none of the variables are shown as having values assigned to them.
 
  • #3
Outstanding! Thank you Mark! I omitted the code in which I assigned values to those variables, but in my actuall code they do have assigned values. I will remember to wrap my code in the future. Thanks again.
 

1. Can I use local variables from one method in another method?

Yes, you can use local variables from one method in another method as long as the variables are declared within the same class. However, the scope of the variable will be limited to the method in which it is declared.

2. How can I access local variables from one method in another method?

You can access local variables from one method in another method by passing them as parameters or by declaring them as instance variables within the class.

3. Is it considered a good practice to use local variables from one method in another method?

It is generally not considered a good practice to use local variables from one method in another method as it can make the code more complex and difficult to understand. It is better to use parameters or instance variables for passing data between methods.

4. Can I change the value of a local variable from one method in another method?

No, you cannot change the value of a local variable from one method in another method. Local variables are declared within a method and their scope is limited to that method only. Any changes made to the variable in another method will not affect the original variable.

5. What happens to local variables after the method execution is completed?

Local variables are destroyed after the method execution is completed. They are stored in the stack memory and are removed once the method finishes its execution. This is why local variables cannot be accessed in another method.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
773
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
995
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top