× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



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
   }
}


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.