Re: Beginner question referring to an object in a class from within method in that class

Peter Dow <[email protected]> Fri, 28 Jul 2023 16:38:20 -0700
Newsgroups gmane.comp.lang.as400.java
Message-ID <[email protected]>
Thanks Barbara. Between you, Craig Pelkie, and robmorning, I'm beginning 
to get it.

If I define it within MyClass, it's similar to a module-level field in RPG.

If I define it within the main() method, it's similar to a 
procedure-level field in RPG.

If I define it within an IF  statement, it's not the same as anything in 
RPG.

The other thing, I'm not certain of this yet, is what constitutes 
defining a field/variable?  At the moment, it looks like specifying the 
type ahead of the field/variable name means I'm defining/declaring it, 
and if I leave off the type, I'm using whatever version of the 
field/variable was defined in the closest scope.

Also, a variable can be defined with the "new" keyword, e.g.

     wb = new SXSSFWorkbook();

without specifying the type, since the type is determined by the 
method's constructor method.


On 7/28/2023 2:55 PM, Barbara Morris wrote:
> 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
>    }
> }
>
-- 
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: [email protected]
Before posting, please take a moment to review the archives
at https://archive.midrange.com/java400-l.