5 Replies Latest reply: Jan 3, 2010 1:00 PM by 843789 RSS

    public static var or passing var along ..?

    843789
      I need some help better understanding when to declare variables as static public and access them throughout my classes and when to pass them along as method parameters? Idea here is that variable travels from one class to the next.

      Say, in my main class i am spending a substantial amount of code on processing user input, validating it etc and at the end based on what i got, i set a boolean variable a=true.

      Now, i know that we're not actually doing anything with 'a' in my main class other then trying to figure out if it is true or false. Another class is being called as part of my code to carry on the operation. At some point action of that other class depends on whether a is set to true or false. So ...

      I see 2 choices here:

      1. declare static public a; in my main class and reference it as if(mainClass.a) {} else {}
      or
      2. pass it along to the processing class from main. someOtherClass(a).

      I tend to favor option 1 heavily and while code works perfectly fine, i wonder if this is a right approach.

      When should value be static and when should value be passes along as a method parameter?
        • 1. Re: public static var or passing var along ..?
          843789
          If you need to access variables from different classes, then use getters/setters to do so. In fact, I'd never declare a variable public unless it's a constant.

          As for declaring it static, that's fine if it's in your main class.
          • 2. Re: public static var or passing var along ..?
            843789
            Would you please provide an example?


            I think I conceptually understand your answer but am not able to visualize it well in context of say 20 distincts variables that are referenced by other classes later on. Do I need 20 setter methods in my main?

            I am sure it will become clear with an example

            Thank you

            Edited by: JRAEL on Jan 3, 2010 9:03 AM
            • 3. Re: public static var or passing var along ..?
              843789
              public class Person {
                  private int age;
                  private int height;
                  private int weight;
              
                  public int getAge() {
                      return age;
                  }
              
                  public void setAge(int age) {
                      this.age = age;
                  }
              
                  public int getHeight() {
                      return height;
                  }
              
                  public void setHeight(int height) {
                      this.height = height;
                  }
              
                  public int getWeight() {
                      return weight;
                  }
              
                  public void setWeight(int weight) {
                      this.weight = weight;
                  }
              }
              • 4. Re: public static var or passing var along ..?
                843789
                Here is a small example of my code below. All came as a result of me processing user submitted arguments and is all in main class

                I declare
                static String whatAreWeDoing;                                             // files or database comparison

                Should this become private String then that is a part of my method below?

                and then whoever is interested in whatAreWeDoing should be calling a getter method? What if another method of the same class is interested in whatAreWeDoing?


                if (arg.equals("-sFile")) {
                     *whatAreWeDoing = "FileComparison"; // SETTER 1*
                               
                                    sourceFile = args[ix + 1];
                
                                    try {
                                         logger.info("Source: " + sourceFile.trim()     + " ... Checking ...");
                                         checkExist(sourceFile.trim());
                                         
                                         Date sourceTimeStamp = new Date(new File(sourceFile).lastModified());
                                         logger.info("Source file was changed on " +  sourceTimeStamp);
                                         
                                         sbDestinationContent.append("#HeaderContent: "
                                                   + OCTutils.neatPath(sourceFile)[0] + " - "
                                                   + OCTutils.neatPath(sourceFile)[1] + "<br><hr><i>" +  sourceTimeStamp + "</i>\n");
                                         
                                         
                                    } catch (FileNotFoundException e) {
                                         //errorMessage.add("FATAL: " + sourceFile     + " is missing");
                                         errorMessage.append("<br><br><br>FATAL: " + sourceFile     + " is missing<br><br><br><br>");
                                    }
                               }
                
                               if (arg.equals("-dFiles")) {
                                    destinationFiles = args[ix + 1].split(",");
                                    dFileArguments = new String[destinationFiles.length];
                
                                    logger.info("Got " + destinationFiles.length + " destination files");
                                    
                                    Date destinationTimeStamp;
                                    for (int i = 0; i < destinationFiles.length; i++) {
                                         String currentDestinationFile = destinationFiles;
                                         
                                         try {
                                              // OCTutils.neatPath is called to handle formatting UI is forcing on OCT
                                              // OCTutils.neatPath(destinationFiles[i])[0] - server name file came from
                                              // OCTutils.neatPath(destinationFiles[i])[1] - file name itself
                                              dFileArguments[i] = OCTutils.neatPath(currentDestinationFile)[0]
                                                        + ":" + OCTutils.neatPath(currentDestinationFile)[1];
                                              logger.info("Destination: " + currentDestinationFile + " ... Checking ...");
                                              checkExist(destinationFiles[i].trim());
                                              
                                              destinationTimeStamp = new Date(new File(destinationFiles[i]).lastModified());
                                              
                                              sbDestinationContent.append("#HeaderContent: "
                                                        + *OCTutils.neatPath(destinationFiles[i])[0]* + " - " // TECHNICALLY THIS IS A GETTER. neatPath "returns"
                                                        + OCTutils.neatPath(destinationFiles[i])[1] + "<br><hr><i>" + destinationTimeStamp + "</i>\n");

                                         } catch (FileNotFoundException e) {
                                              // this is a last occurrence of errorMessage
                                              //errorMessage.add("FATAL: " + currentDestinationFile + " is missing");
                                              errorMessage.append("<br><br><br>FATAL: " + currentDestinationFile + " is missing<br><br><br><br><br>");
                                         }
                                    }
                               }
                Based on what you see above, consider that virtually each and every parameter is being used by methods in other classes, what setter/ getter methods would you  recommend? Can i assume that i need no setters as all values are assigned                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                • 5. Re: public static var or passing var along ..?
                  843789
                  Should this become private String then that is a part of my method below?
                  If whatAreWeDoing can be accessed via a getter/setter, then yes, declare it as private.
                  and then whoever is interested in whatAreWeDoing should be calling a getter method?
                  Yes.
                  What if another method of the same class is interested in whatAreWeDoing?
                  It doesn't matter.
                  Can i assume that i need no setters as all values are assigned
                  Yes, you don't need setters unless you need to reassign values.