Search code examples
naming-conventionsaccess-modifiers

Naming public static final in lowercase?


I have a hierarchy of classes:

class Page {
}
class ChildPage1 extends Page {
    private static final String URL;
}
class ChildPage2 extends Page {
    private final String url;
}

Some pages have static urls but other don't. I need some methods like load() in parent class Page that will use url of it's descendant. I thought about implementing it in these ways:

  • using reflection api
  • naming static final URLs in lowercase (in conroversary to naming conventions).

Is it worth to not follow naming conventions in this case?


Solution

  • You can solve your problem by adding an abstract method getUrl() to Page that has to be implemented in ChildPage1 and ChildPage2.

    class Page {
        protected abstract String getUrl();
    }
    
    class ChildPage1 extends Page {
        private static final String URL;
    
        protected String getUrl() {
            return URL;
        }
    }
    
    class ChildPage2 extends Page {
        private final String url;
    
        protected String getUrl() {
            return url;
        }
    }