Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [local-variable]

A variable whose scope is contained to a smaller unit of code such as a function or class.

32
votes
8answers
7k views

Does “variables should live in the smallest scope as possible” include the case “variables should not exist if possible”?

According to the accepted answer on "Rationale to prefer local variables over instance variables?", variables should live in the smallest scope possible. Simplify the problem into my interpretation, ...
-1
votes
2answers
244 views

Is there any consequences to write for(float i=0, myFloatVar=0; i < n; i++)?

For example, sometimes I need a float variable to be used inside a for loop, e.g.: float sum=0; for(int i = 0; i < students.length; i++) { sum += Math.random(); students[i].result = sum; } ...