1

I am developing a small app while learning Android.

The app is basically making a series of simple math calculations. A button is calling a function where the calculations take place. Everything was working fine, until I inserted an if/else construct.

Inside this construct, I am using variables created before, making calculation and setting other variables with this

if (TS>Ex) {
    Double AE = 0.00;
} else {
    Double AE = (Ex-TS);
};

Double TBTAT = (TS-Ex);
Double Exx = 2864.17;   
if (TBTAT>Exx) {
    Double TAT = (Exx*0.2);
} else {
    Double TAT = (TBTAT*0.2);
};

I have two of these if/else structures.

Then everything is collected and sent to a Text

IT_ResultTXT.setText(Double.toString(AE+TAT+TAF));

In normal conditions, AE, TAT, TAF turn out to "cannot be resolved to a variable" in this last line of the code, but if I declare them at the beginning of the function, I have an error of duplicated variables.

I suppose is a very stupid basic Java programming error, but I cannot find a solution to this.

3
  • Give this a read, it should help: java-made-easy.com/variable-scope.html Commented Jun 16, 2012 at 22:48
  • If you declare a variable inside a bracket set like {}, it is only visible inside those brackets. Commented Jun 17, 2012 at 0:22
  • Thanks all of you for the answers! -Thanks kcoppock for the link Commented Jun 17, 2012 at 16:23

2 Answers 2

8

You have to declare at the beginning (or somewhere else in enclosing scope), only once:

Double AE;

if (TS > Ex) {
    AE = 0.00;
} else {
    AE = (Ex - TS);
};

Double TBTAT = (TS - Ex);
Double Exx = 2864.17;    

Double TAT;

if (TBTAT > Exx) {
    TAT = (Exx * 0.2);
} else {
    TAT = (TBTAT * 0.2);
};
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Darshan, if I do like that, i have an error of "duplicated variable AE"
Not true. Copy paste the code and you'll see that you don't get it.
Darshan and lxx you are right, sorry for that I understand i was declaring via "double var = whatever" instead of "var = whatever", is that the point? So, in this case, what kind of variable will i get? Thank you very much for your answers!
The variable is valid (and has to be unique) inside it's scope... in the case of if else you have 2 scopes, if you declare the variable inside each scope you have 2 separate variables, which don't affect each other (separate namespaces), you will not get "duplicate variable" error. But then you can't access the variable outside the scope... read about it, everything can't be explained this short...
When you do "var = whatever" you are assigning the variable. With "double var" you are declaring the variable. "double var = whatever" is both declaration and assignement at once. Your declared variables have to be unique in it's scope, so you can declare them only once in the scope. But you can assign (change the value) as offen as you wish.
0

The scope of AE, TAT, TAF are only within the if/else statement block, and are not visible outside.

Its better to declare it as local varible to the method in which these if/else statement is present or as class intance variables

As local varibles:

    public void go() {

    Double AE = 0.00;
    Double TBTAT = (TS-Ex);
    Double Exx = 2864.17;  

    if(condition){


      }

   else{



      }

}


   As Class scope:

             public class A {

               Double AE = 0.00;
               Double TBTAT = (TS-Ex);
               Double Exx = 2864.17; 


            public void go(){


                if (condition){



              }
                else{


                }



              }


         }

2 Comments

I didn't really got your point Kumar The first case is the one I already have, the second maybe too advanced for me. Shall I then make a class for every calculation? Can I passthe variable value out of the statement anyway?
No its not like that.i mean the Scope.When you declare a variable at Class scope, you are making it visible everywhere into every method of the class. And if its inside the method scope, then its visible into every try-catch, loop etc within that method, now i want to include one more thing.its about inner classes.when you declare a local class,that mean a class within the method, then if that class needs to access the variable in the method.that needs to be declared final.well if you didnt get the inner class example, leave it.but i think you will have a clear view of the above two examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.