Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Good Coding Practice

  1. #1
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Good Coding Practice

    Hello All,

    I new to this Forum and as well to Java. I am a learner in Java and want to the best things always. While writing the line System.out.println("Value is" + val); Here I am using the String "Value is" directly in my code itself. When ever in future, if Want to change this string I have to edit the code. Is there any way I can call such type of strings from another file/.java class (this file should contain only such type strings), so that I can edit the value in this file once, which subsequently changes where ever I am calling this string in the code.

    This can save a lot of time in searching for a string that needs to be replaced at several places inside Java code. I have seen such things in .NET. Kindly direct me to any Tutorial. An explanation with Example will be more helpful to me.

    Thanks,
    Dinesh

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Good Coding Practice

    You could just have a class called Constants that references static constants.

    Java Code:
    public class Constants {
      private Constants(){}
      public final static String VALUE = "Value is ";
    }
    Then
    Java Code:
    System.out.println(Constants.VALUE + val);
    That is an acceptable way of doing it but probably the best would be to use enums.

    Regards,
    Jim
    Last edited by jim829; 09-22-2013 at 05:13 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Good Coding Practice

    Have a look at the ResourceBundle class; Strings can be resources. The mechanism works fine for localization too.

    kind regards,

    Jos
    jim829 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Good Coding Practice

    Actually, I like this better. Then you don't have to recompile the code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Good Coding Practice

    Thanks Jim and Thanks Josh for your Replies.

Similar Threads

  1. Good Practice Programming Websites?
    By littlebirdpoo in forum New To Java
    Replies: 1
    Last Post: 02-18-2012, 03:58 PM
  2. Replies: 4
    Last Post: 10-25-2010, 04:49 PM
  3. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 04:32 PM
  4. Good real world practice
    By Mr.Beans in forum Jobs Discussion
    Replies: 1
    Last Post: 08-15-2009, 05:59 AM
  5. [SOLVED] Is this good programming practice?
    By porchrat in forum New To Java
    Replies: 9
    Last Post: 04-08-2009, 01:54 PM

Tags for this Thread

Posting Permissions

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