|
On 2023-07-27 6:56 p.m., Peter Dow wrote:
I'm very much a beginner with Java and I'm having a problem understanding how scope works.
...
If you define a variable inside a block, it's only visible within that block.
Here, my variable "str" can't even be used in main() outside the "if" block where I defined it.
class MyClass
{
static public void main()
{
if (1 == 2)
{
String str = "abc"; // only visible in this "if" block
str = "def"; // ok, still in the "if" block
}
str = "ghi"; // str is not visible any more
}
}
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.