+ 1

Can we declare local variables in static function if no then how can we use them in static void main?

in which scenario I can't use local variables inside static function?

21st Jun 2018, 9:13 AM
Mayur Bagul
Mayur Bagul - avatar
2 ответов
+ 1
Lets say for Java. Local variables in static methods are not static; they are just local variables. Static variables are held in memory attached to the corresponding Class objects; any objects referenced by static reference variables just live in the regular heap. When you pass a static variable to a method as an argument... absolutely nothing interesting happens. Source: https://stackoverflow.com/questions/10645914/are-local-variables-in-static-methods-also-static Credit: https://stackoverflow.com/users/659804/ernest-friedman-hill Maybe these can help too: 1) https://beginnersbook.com/2013/05/static-variable/ 2) https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/
21st Jun 2018, 9:38 AM
Billy Tsouvalas
+ 1
" Can we declare local variables in static function" Of course. Static functions in a class or global functions have the same property as regular functions when we are talking about the scope inside their body. Functions, in general, are considered black boxes to the outside world of their body. Whatever you put inside { .... } are visible/usable/accesable to just that function. " if no then how can we use them in static void main?" There's no such thing like static void main() at least in C/C++ " in which scenario I can't use local variables inside static function?" There's no point to not using a variable when you declare it, unless it is being affected by a conditional block inside the function.
21st Jun 2018, 9:52 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar