0

Is it possible to initialize a same variable with same name again and again in different functions...??

7th Jul 2016, 7:10 AM
Stephen Yuvaraj
Stephen Yuvaraj - avatar
3 Réponses
+ 1
Each variable is tied to the method it is created so yes you could but it may get confusing with large programs seeing the same name in all your methods. Always best to name variables for their specific purpose. Say you need a spare int x in several methods. It would help to name it something like addX for am addition method, SubX for subtraction. Makes it more readable
7th Jul 2016, 7:26 AM
James
James - avatar
+ 1
Also, if you need one variable for multiple methods, you could create it as static outside all methods, like underneath the class name. This way, you don't have to keep creating a new variable each time
7th Jul 2016, 7:31 AM
James
James - avatar
0
If you read scope of variables it will be helpful
6th Aug 2016, 10:41 AM
Suyash Kathar
Suyash Kathar - avatar