What is the scope of the variables declared in the class constructor?

I was curious what is the scope of variables declared inside the constructor of classes that are not data members of this class?

For example, if a constructor needs an iterating int i, will this variable be destroyed after the constructor completes, or is it global for the program?

Thanks!

+3
source share
6 answers

In this sense, the constructor is like any other function - any variable declared inside has the usual limitations in scope, and all of them necessarily go beyond the scope and are destroyed after the constructor completes.

+11
source

, , "" . .

+6

, , .

, , ( , , ): , ( "", " " ).

am, , , ( , , ): , .

+5

, , .

public MyClass() {
   int i = 0; // i is only available inside this constructor.
              // It can't be used in any other function of this class or any other.
}
+3

, , , . .

+2

The area can be either static (lexical) or dynamic. Most languages ​​use a lexical region, which means that the region is defined by the program text (for example, "inside the set of curly braces where it is defined"), and not by the value of what you wrote.

http://en.wikipedia.org/wiki/Scope_(programming)

+1
source

Source: https://habr.com/ru/post/1705476/


All Articles