Results 1 to 5 of 5
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default need opinion on public static final etc ...

    Everything works as intended. I am looking for conceptual advise. Am i doing it the right way from design stand point?

    Suppose a
    Java Code:
    public class A
    contains the following declarations:

    Java Code:
        // delimiters
        public final static char DELIMITER = '\u0002';
        public final static char ERROR_SIGN = '\u0003';
    So why public, why final, why static?

    Idea was that since A is the first class that encounters these variables, it is a right place to declare them at. It is public, because going in, i thought other classes will take advantage of this declaration. It is static because only a single instance of it exists in my universe and it is final, because it will never change

    That being said, after declaration, public class A later continues to use them locally.

    As mentioned before, this first time these variables are introduced ... Until set here at A, no other class needs to worry about them.

    Moving on .. here comes
    Java Code:
    class B
    Say it uses A.DELIMITER 20 times and A.ERROR_SIGN 10 times

    Moving on .. here comes
    Java Code:
    class C
    This guy uses these delimiters to split a string 300,000,000 (not really, but plenty) times so it does this

    Java Code:
    // do translation to String once instead of tons of times below
        private String errorSign = (Character.toString(A.ERROR_SIGN));
        private String delimiter = (Character.toString(A.DELIMITER));
    After this class C continues to refer to local errorSign and delimiter

    And so, i wonder ... is this a right approach? Should class A be the class where public static final vars are declared? Should i make a new separate class for this? Is approach i followed in class C appropriate?

    Generally speaking, how do you deal with something like this?

    Please advise and thanks for reading

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    the approach is good. Keeping all constants in one place is a good practice. I would move string constants over there as well:

    Java Code:
    public class MyConstants {
      private MyConstants() {
        // don't want anyone to make instances of this
      }
      public final static char DELIMITER = '\u0002';
      public final static char ERROR_SIGN = '\u0003';
    
      public final static char STRING_DELIMITER = Character.toString(DELIMITER);
      public final static char STRING_ERROR_SIGN = Character.toString(ERROR_SIGN);
    }
    and then everyone uses

    Java Code:
    MyConstants.STRING_DELIMITER
    etc.

    One thing with this is that typing "MyConstants." everywhere is pretty tedious. Here is what you can do to avoid that:

    Java Code:
    public [B]interface[/B] MyConstants {
      // yes, you can define constants in an interface!
      public final static char DELIMITER = '\u0002';
      public final static char ERROR_SIGN = '\u0003';
    
      public final static char STRING_DELIMITER = Character.toString(DELIMITER);
      public final static char STRING_ERROR_SIGN = Character.toString(ERROR_SIGN);
    }
    
    class A implements MyConstants {
      public void stuff() {
        if('!' == DELIMITER) {
          //...
        }
      }
    }

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Or how about considering using an enum here?

  4. #4
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Nothing short of "awesome" Thanks Iljuxa. спасибо

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,808
    Rep Power
    19

    Default

    Quote Originally Posted by curmudgeon View Post
    Or how about considering using an enum here?
    Possibly, but logically speaking are DELIMITER and ERROR_SIGN part of the same enumeration? Having a UsefulCharacterConstantsEnum seems wrong to me, somehow.

Similar Threads

  1. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 11:58 AM
  2. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  3. Public static method error
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •