Results 1 to 7 of 7
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default Problem accessing a constant defined in another class

    I have two classes

    public class SearchAction
    public class SpmlConstants

    both in the same package.

    In SearchAction, somewhere I have this line

    Java Code:
    objectClassElem.appendChild(doc.createTextNode([B][COLOR="Red"]SpmlConstants.TEMPLATE_SUBSCRIBER_ELEMENT[/COLOR][/B]));
    In SpmlConstants I have

    Java Code:
    public final static String TEMPLATE_SUBSCRIBER_ELEMENT = "TemplateSubsbriber";
    Note that I have misspelled TemplateSubsbriber.

    I built both the class files and patched it on to my machine and ran the test case. The SPML generated by SearchAction obviously had the wrong String TemplateSubsbriber.

    I realized my mistake and corrected the spelling in SpmlConstants.java to TemplateSubscriber, built and re-patched SpmlConstants class.
    Still i get the same error. Then just to try my chances I patched SearchAction class also. Now it worked.

    Could someone please explain why was it required to build SearchAction class also, even though the constant is defined in SpmlConstants and only a public static member of that class was used in SearchAction:confused:

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Could someone please explain why was it required to build SearchAction class also

    My understanding is that if you have a so called constant variable (a final primitive or string that is initialised with a compile time constant) then what gets put into the resulting class file is the value itself and not a reference to the other class.

    Compile time constants are defined in the JLS in the section 15.28 Constant Expression. And 13.1 The Form of a Binary contains the statement "References to fields that are constant variables (4.12.4) are resolved at compile time to the constant value that is denoted". CHapter 13 on binary compatability is quite interesting.
    Last edited by pbrockway2; 01-26-2011 at 04:21 AM.

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Unhappy

    Yes, I understood. Even I guessed the same thing.

    But I have around 20 class files in my project, spread across different packages and all my constants are defined in SpmlConstants.java. Suppose, in future I want to change the String value of another constant, then I need to check which all classes among those 20 classes use this constant and I have to build those files also. So, is this the right way to define a constant and use it across other files?

    I thought of using getter methods in SpmlConstants, but that too wont be a good design because I have around 50 constants defined in that file. So I'll need 50 getters.

    Could anyone please suggest a solution for this, so that I have to build the SpmlConstants.java ONLY?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I thought of using getter methods in SpmlConstants, but that too wont be a good design because I have around 50 constants defined in that file. So I'll need 50 getters.

    Chapter 13 of the JLS (previously linked to) recommends precisely this: 50 private constants and 50 getters.

    From 13.4.9 "Other than for true mathematical constants, we recommend that source code make very sparing use of class variables that are declared static and final. If the read-only nature of final is required, a better choice is to declare a private static variable and a suitable accessor method to get its value".

    They do mention, but do not recommend

    Java Code:
    public interface SpmlConstants {
        TEMPLATE_SUBSCRIBER_ELEMENT = new String("TemplateSubsbriber");
    }

    Since they don't recommend it I haven't bothered to try it.

    Perhaps you could consider enums.

  5. #5
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Thanks, I think enums would be a better choice.
    If others have a different opinion, please post it.

  6. #6
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Thumbs up thanks

    Using enums solved the issue

    Java Code:
    public class SpmlConstants {
    	[I][COLOR="Olive"]//public static final String TEMPLATE_SUBSCRIBER_TAG = "TemplateSubscriber";[/COLOR][/I]
    	public enum ConstantsEnumeration {
    		TEMPLATE_SUBSCRIBER_TAG ("TemplateSubscriber"),
    		SUBSCRIBER_TAG	  	("Subscriber")
    		[COLOR="olive"][I]// ---more constants---[/I][/COLOR]
    		;
    
    		private final String tagName;
    
    		ConstantsEnumeration(String tagName) {
    			this.tagName = tagName;
    		}
    
    		public String getTagName() {
    			return tagName;
    		}
    	}
    }
    In the other file I used
    Java Code:
    SpmlConstants.ConstantsEnumeration.SUBSCRIBER_TAG.getTagName();
    Thanks for your help.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Replies: 0
    Last Post: 01-25-2011, 06:02 AM
  2. Accessing class defined within a constructor
    By kwaspl in forum New To Java
    Replies: 4
    Last Post: 12-21-2009, 02:35 PM
  3. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 09:05 PM
  4. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  5. How to call defined java class in Jsp
    By asheeshiit in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-04-2009, 04:21 AM

Posting Permissions

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