Question

How would I use a variable declared within an if statement in another method of the...

How would I use a variable declared within an if statement in another method of the same class??

Ex.

public static void declaration_st(ArrayList E) {
    if (look("float")) {
        String funcID = tokM1(E).getToken(); //I Want to use this funcID in another method 
        Juccept("float");
       
    } else if (look("int")) {
        String funcID = tokM1(E).getToken(); //or if the program gets to an int I want it to use this funcID from this if statement in another method

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
If you want to use that variable inside a different method, then you probably should think of making that variable as a field of the class.

class Cls {
    private String funcID; 

    public static void declaration_st(ArrayList E) {
        if (look("float")) {
            funcID = tokM1(E).getToken();   // now you can access funID from any method. 
            Juccept("float");
        } else if (look("int")) {
            funcID = tokM1(E).getToken();
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
How would I use a variable declared within an if statement in another method of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT