Re: Beginner question referring to an object in a class from within method in that class
Barbara Morris <[email protected]> Fri, 28 Jul 2023 17:55:34 -0400
| Newsgroups | gmane.comp.lang.as400.java |
|---|---|
| Organization | IBM |
| Message-ID | <[email protected]> |
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
}
}
--
Barbara
--
This is the Java Programming on and around the IBM i (JAVA400-L) mailing list
To post a message email: [email protected]
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/java400-l
or email: JAVA400-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org
Before posting, please take a moment to review the archives
at https://archive.midrange.com/java400-l.