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

robmorning--- via JAVA400-L <[email protected]> Thu, 27 Jul 2023 23:11:49 +0000 (UTC)
Newsgroups gmane.comp.lang.as400.java
Message-ID <[email protected]>
 It looks like you've defined the setFont() function in the main method itself and not the class .. Move the 2nd last brace ( } ) to just after the System.exit   ..  Also the FontDetail is defined only within the main method  .. it can't be accessed from outside of this method ..  

    On Thursday, 27 July 2023 at 23:56:32 BST, Peter Dow <[email protected]> wrote:  
 
 I'm very much a beginner with Java and I'm having a problem 
understanding how scope works.

In MyPgm below, I get an error saying that fontDetail in the setFont() 
method is undefined.

 From what I've read in the documentation, a reference within the 
setFont() method to a field within the same class but outside the method 
is allowed.

Is fontDetail a field or a variable? Is it only visible within the 
main() method, but not within any methods defined in main()?


public class MyPgm {

   public static void main (String[] parameters) {

     if (parameters.length == 2) {
       String FONTtype    = parameters[0];
       String fontDetailSize = parameters[1];

         wb = new SXSSFWorkbook();
         Font fontDetail = wb.getFontAt((short)0);
         setFont();

     } else {
         System.out.println("Wrong number of parameters.");
         System.exit(1);
     }
     System.exit(0);

     public static void setFont() {
         fontDetail.setFontName(FONTtype); <<=====fontDetail undefined
     }
   }
}

--
*Peter Dow* /
Dow Software Services, Inc.
909 793-9050
[email protected]
[email protected] /
-- 
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.
  
-- 
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.