What is the scope of a variable that is declared within a block of code that is defined within a method and which is a subset of the statements that make up the method?
Advertisements
प्रश्न
What is the scope of a variable that is declared within a block of code that is defined within a method and which is a subset of the statements that make up the method?
दीर्घउत्तर
Advertisements
उत्तर
- The scope of a variable declared within a block of code inside a method is known as block scope.
- In Java, a block of code is enclosed within curly braces
{ }. A variable declared inside such a block is called a block-local variable. It can be accessed and used only within the block in which it is declared. - The variable becomes accessible from its declaration point and remains accessible until the closing brace
}of that block. It cannot be accessed outside the block, even if the block is part of the same method.
Thus, the scope of a block-local variable is restricted to its own block rather than the entire method.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
