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

Click For Summary
SUMMARY

The discussion focuses on the challenge of using local variables from one method in another within the same Java class without converting them into class fields. The user seeks a solution that adheres to their instructor's guidelines against adding new fields. The recommended approach is to utilize method parameters to pass the values of local variables, ensuring that these variables are assigned values before being passed. This method maintains encapsulation and adheres to best practices in Java programming.

PREREQUISITES
  • Understanding of Java method parameters and return types
  • Familiarity with Java ArrayLists and their usage
  • Basic knowledge of Java class structure and variable scope
  • Experience with Java control structures, such as loops and conditionals
NEXT STEPS
  • Learn about Java method overloading and how to use multiple methods with similar names
  • Explore Java's encapsulation principles and the use of getters and setters
  • Study Java debugging techniques to track variable values during execution
  • Investigate Java best practices for code organization and readability
USEFUL FOR

Java developers, programming students, and anyone looking to improve their understanding of variable scope and method interactions in object-oriented programming.

wyosteve
Messages
23
Reaction score
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
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.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
5K