Java Constants

Hi all,
Is normal class or abstract class or interface a good place for declaring constants?What is best practices for constants?
Regards
Ranjeet

constants, otherwise I agree with
CeciNEstPasUnProgrammeur :)Kaj, you may call me Rene.
There are times when you want to declare constants in
an interface. Especially if they are used as
arguments (or returnvalues) for some of the methods
in the interface. E.g.My thoughts exactly.
Ulrika, I feel a a bit uneasy about having "global constants". My constants usually are only relevant to a class (or an interface), or to those who call it. Meaning: from what I saw so far, constants have a defined and endemic scope of context. If you place them in some "storage" class, you remove them from that context. Additionally: a class without methods or attributes is a design smell to me. ;-)
If a certain data set or constant is to be shared among different parts of the app, creating a property bundle is what I'd do.

Similar Messages

  • JavaEditorKit sun.tools.java.Constants jar help

    I found a good article on JavaEditorKit using JEditorPane.
    Problem is this article uses sun.tools.java.Constants. I could not find a jar that would provide the functionality.
    I was able to see api for sun.tools.java.Constants.
    I need someone to help me in getting the approriate jar.
    Thanks & Regards
    Ajay Kalidindi

    Hi,
    I saw that too thinking it would be a good way to learn about a simple editor in Java.
    I got it working only after upgrading the code to Java 2 (ie com.java to javax) and extracting out all of sun.tools.java package from tool.jar out of my jdk. It looked ok but I think this is way too old example to be using now days.
    I think that the sun.tools.java package has been protected from developers use so it wouldn't help you
    develope anything anyway, however http://www.jext.org uses a similiar idea to do its' syntax highlighting
    it might worth checking out.
    regards
    David

  • Any idea where I can find sun.tools.java.Constants ?

    I've been playing around with all that syntax highlighting nonsense which seems to be a rite of passage around here. I came across the Sun version but they assume I know where to find
    sun.tools.java.Constants
    which, of course, I don't. Any ideas on where I can get this class?

    Thanks. I thought I'd looked there... still, only three bucks. Mind you, that example still isn't working.

  • Java - Constant Network Activity - Eating up Internet Bandwidth!

    Hi,
    The Process "java"is constantly sending data (100-140 KB/sec) and receiving Data (2-4 KB/sec) 24x7.
    I have terminated all "cloud" applications (iCloud, Google Drive, Dropbox and CrashPlan etc.). The total
    Data sent is about 150 MB in less than 15 mins after a complete reboot eating up my internet bandwidth.
    Upgraded to 10.8.3 and also the March 4th Apple update for Java 6.
    Called AppleCare and they acknowledged the issue, but sent me off to a Java customer support line
    who then turned me down (as I was not a licensed/commercial user of Java). Applecare was useless.
    Anyone else having the same issue? How can I completely uninstall Java? Any help or insight is appreciated.
    BR,
    Raj

    Bingo! That was it. Many thanks for connecting CrashPlan and Java. I thought exiting the CrashPlan application would turn this backup service off. But as you suggested CrashPlan runs a Java Daemon/Engine in the background even after exiting the CrashPlan application.
    The below command effectively shutsdown the CrashPlan's Java background Engine and the network activity ceased immediately.
    sudo launchctl unload /Library/LaunchDaemons/com.crashplan.engine.plist
    (From CrashPlan's support site: http://support.crashplan.com/doku.php/recipe/stop_and_start_engine )
    Many thanks Linc Davis.

  • Using Java constant in JSF

    Hello!
    I�m new in JSF and I want to use a constant into a jsf tag but I don�t know how to do it. I know in JSTL but not in FSF.
    This is what I want:
         <h:outputText id="test"     value="<%Constantes.DOCUMENT%>"/>     I have tried so many things to use my constant in the value element but I don�t know the way                    
    Thanks.

    first off Scriplets are bad practice in JSF and should be avoided.
    I use this workaround for constants in jsf. Create a my constant as properties on a FacesConstants class, which is defined as a Managed Bean with scope of none and having getters for each property.
    public class FacesConstants implements MyConstants{
    public String document = DOCUMENT;
    public String getDocument(){
    return document;
    <managed-bean>
         <description>used to get faces constants for refereing in EL Expresssions</description>
              <managed-bean-name>constants</managed-bean-name>
              <managed-bean-class>com.myproj.faces.util.FacesConstants</managed-bean-class>
              <managed-bean-scope>none</managed-bean-scope>
    </managed-bean>
    so in the jsf code just say #{constants.document}
    Not sure if this is best practice? in fact I'd prefer a more elegant solution
    Edited by: scottyb on Apr 8, 2008 5:10 AM
    Edited by: scottyb on Apr 8, 2008 5:12 AM

  • Using a Variable from web-inf/web.xml in java code?

    I would like to create a read-only variable with a value in the /web-inf/web.xml file and then access that value within a java service method within the Application Module.
    Where / how do I read the value from the web.xml file and make it accessible within my java code?
    thanks

    Hi,
    you can define context parameters in web.xml that you can access from Java in the web application
    The following is a JSF examle that checks a context parameter in a PhaseListener, where the string is stored ina Java constant (could be "myParam")
    ExternalContext exctx = FacesContext.getCurrentInstance().getExternalContext();
    useSessionPageCache = exctx.getInitParameter(Constants.USE_PAGE_CACHE) == null ? false :
    (exctx.getInitParameter(Constants.USE_PAGE_CACHE)).equalsIgnoreCase("TRUE") ? true : false;
    From here you can call a method on the AM to set the value or look for another option.
    Frank

  • Accessing Constant classes from jsps

    org.apache.struts.taglib.html.Constants is a java constant class with no getter/setter methods
    is this the only way (not using other form beans) to get the value of a constant within that class
    can we use pageContext or any other expression language to do things like this?
    <c:set var="tokenKey">
    <jsp:expression>
    org.apache.struts.taglib.html.Constants.TOKEN_KEY
    </jsp:expression>
    </c:set>
    thank you

    EL doesn't have any way to access constants.
    What you have there is pretty much the only method without using other beans.
    There are certain workarounds that have been proposed. Javaranch had an article on it a while ago:
    http://www.javaranch.com/journal/200601/Journal200601.jsp#a3
    A previous thread on this topic, with a proposed solution: http://forum.java.sun.com/thread.jspa?threadID=508847

  • Constants vs Properties

    Hi there,
    We currently use properties files for the storage of error messages something like this: ...
    EM53=Invalid account number
    ...This all works fine, but I was wondering about the performance issues associated with reading properties, and I wondered about the advantages of using Java constants to store these values instead (say in an interface).
    String EM53 = "Invalid account number";Now the obvious issue with this, is how to get from a string containing the error code ("EM53" in the example) to the actual error message. This is where Reflection could be used. But then there are performance implications to using Reflection to get the value of a field too.
    Please note that the software will only ever be used by one company in English speaking countries and it is extremely unlikely that we would require multiple versions of these messages. Also note that the current deployment method of new releases of the software, precludes the deployment of individual jars or files.
    Obviously I can test the performance implications for myself easily enough, but would anyone like to comment on these alternative approaches from a design or maintenance perspective?
    Thanks,
    Tim

    If you wanted to have variables you could access,
    then something simple like this would
    work:public class Messages {
    public static final String EM53 = "Invalid account
    nt number";Yep that was what I was suggesting. The issue is that we pass back the error code from the server to the client, so we need a way of converting from a string code into the constant name to get the string value for display. That was what I was suggesting the use of Reflection for.
    uses it. So changing the value of that string would
    require recompiling every class that used it, to get
    the new value into those classes.True - we are aware of that. Messages change very infrequently and if we're using Reflection to read the constant then this doesn't apply anyway.
    quantify them. You only read a properties file once,
    when the program starts up (at least I hope that's
    how your code works). After that you're just doing a
    lookup in a hash table to get the error message,This is a good point. I'll have to think about this.
    Thanks,
    Tim

  • Java.lang.ClassLoader.defineClass0

    Hi
    I have JDK1.3 installed in Windows NT4.0 with Service Pack. I am trying to compile a small RMI code I have three files in
    C:\xml
    1. IHelloRmi.java // This is the remote inteface.
    2. HelloRmi.java // This is the implementation
    3. HelloRmiClient.java // This is the client
    All files have "package rmi" statement at top.
    I compiled em succesfully. Now I want to create a stub.
    I give following commands
    C\xml> rmic rmi.HelloRmi
    C\xml\rmi> rmic rmi.HelloRmi
    but it gives following error
    Exception in thread "main" java.lang.NoClassDefFoundError: sun/tools/java/Constants
    at java.lang.ClassLoader.defineClass0(native method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
    etc.....
    Any help will be appreciated

    Hi,
    I am assuming that you have C:\xml in your classpath. Create a sub directory called rmi and put these 3 files in this sub -directory. You should not have any problem in compiling and creating stubs.
    Check whether your .jar files has been placed in classpath .
    Hope this will help you.
    Anil.
    Developer Technical Support
    Sun Microsystems Inc,
    http://www.sun.com.developers/support

  • Document about java portlet CSS class

    Is there a document to list/explain all the available CSS classes available to java portlet?
    For example I know that I can use:
    <span class="PortletHeading1">Name</span>
    Whatelse?
    Thanks.

    Here you find the PDK-Java constants (and this one is for PL/SQL).
    In case you're interested in the internal names of the font classes, take a look at the CSS generated for your portal pages. There is a reference to it somewhere on the top of your portal pages:
    <link REL=Stylesheet TYPE="text/css" HREF="CSS_URL">Copy the <CSS_URL> into your browser and it lists all the classes and their definitions for you.
    Hope it helps,
    Peter

  • Oracle error codes

    Hello,
    My application is handling exceptions like this:
    try{
    resultSet = pStatement.executeQuery();
    catch(SQLException e){
    conn.close();
    if(e.getErrorCode() == 942)
    // handle with table or view doesn't exist
    else
    // handle with other errors
    Is there a way to not hard code the error codes?
    Is there any java constants to oracle error or messages in the jdbc driver?
    Thanks,
    Vladimir.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Marcus Uddenhed ([email protected]):
    I need to know where i can find,
    a description to the error codes
    in Oracle.<HR></BLOCKQUOTE>
    null

  • Difference between Object equals() method and ==

    Hi,
    Any one help me to clarify my confusion.
    stud s=new stud();
    stud s1=new stud();
    System.out.println("Equals======>"+s.equals(s1));
    System.out.println("== --------->"+(s==s1));
    Result:
    Equals ======> false
    == ------------> false
    Can you please explain what is the difference between equals method in Object class and == operator.
    In which situation we use Object equals() method and == operator.
    Regards,
    Saravanan.K

    corlettk wrote:
    I'm not sure, but I suspect that the later Java compilers might actually generate the same byte code for both versions, i.e. I suspect the compiler has gotten smart enough to devine that && other!=null is a no-op and ignore it... Please could could someone who understands bytecode confirm or repudiate my guess?Don't need deep understanding of bytecode
    Without !=null
    C:>javap -v SomeClass
    Compiled from "SomeClass.java"
    class SomeClass extends java.lang.Object
      SourceFile: "SomeClass.java"
      minor version: 0
      major version: 49
      Constant pool:
    const #1 = Method       #4.#15; //  java/lang/Object."<init>":()V
    const #2 = class        #16;    //  SomeClass
    const #3 = Field        #2.#17; //  SomeClass.field:Ljava/lang/Object;
    const #4 = class        #18;    //  java/lang/Object
    const #5 = Asciz        field;
    const #6 = Asciz        Ljava/lang/Object;;
    const #7 = Asciz        <init>;
    const #8 = Asciz        ()V;
    const #9 = Asciz        Code;
    const #10 = Asciz       LineNumberTable;
    const #11 = Asciz       equals;
    const #12 = Asciz       (Ljava/lang/Object;)Z;
    const #13 = Asciz       SourceFile;
    const #14 = Asciz       SomeClass.java;
    const #15 = NameAndType #7:#8;//  "<init>":()V
    const #16 = Asciz       SomeClass;
    const #17 = NameAndType #5:#6;//  field:Ljava/lang/Object;
    const #18 = Asciz       java/lang/Object;
    SomeClass();
      Code:
       Stack=1, Locals=1, Args_size=1
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
      LineNumberTable:
       line 1: 0
    public boolean equals(java.lang.Object);
      Code:
       Stack=2, Locals=2, Args_size=2
       0:   aload_1
       1:   instanceof      #2; //class SomeClass
       4:   ifeq    25
       7:   aload_1
       8:   checkcast       #2; //class SomeClass
       11:  getfield        #3; //Field field:Ljava/lang/Object;
       14:  aload_0
       15:  getfield        #3; //Field field:Ljava/lang/Object;
       18:  if_acmpne       25
       21:  iconst_1
       22:  goto    26
       25:  iconst_0
       26:  ireturn
      LineNumberTable:
       line 6: 0
    }With !=null
    C:>javap -v SomeClass
    Compiled from "SomeClass.java"
    class SomeClass extends java.lang.Object
      SourceFile: "SomeClass.java"
      minor version: 0
      major version: 49
      Constant pool:
    const #1 = Method       #4.#15; //  java/lang/Object."<init>":()V
    const #2 = class        #16;    //  SomeClass
    const #3 = Field        #2.#17; //  SomeClass.field:Ljava/lang/Object;
    const #4 = class        #18;    //  java/lang/Object
    const #5 = Asciz        field;
    const #6 = Asciz        Ljava/lang/Object;;
    const #7 = Asciz        <init>;
    const #8 = Asciz        ()V;
    const #9 = Asciz        Code;
    const #10 = Asciz       LineNumberTable;
    const #11 = Asciz       equals;
    const #12 = Asciz       (Ljava/lang/Object;)Z;
    const #13 = Asciz       SourceFile;
    const #14 = Asciz       SomeClass.java;
    const #15 = NameAndType #7:#8;//  "<init>":()V
    const #16 = Asciz       SomeClass;
    const #17 = NameAndType #5:#6;//  field:Ljava/lang/Object;
    const #18 = Asciz       java/lang/Object;
    SomeClass();
      Code:
       Stack=1, Locals=1, Args_size=1
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
      LineNumberTable:
       line 1: 0
    public boolean equals(java.lang.Object);
      Code:
       Stack=2, Locals=2, Args_size=2
       0:   aload_1
       1:   instanceof      #2; //class SomeClass
       4:   ifeq    29
       7:   aload_1
       8:   ifnull  29
       11:  aload_1
       12:  checkcast       #2; //class SomeClass
       15:  getfield        #3; //Field field:Ljava/lang/Object;
       18:  aload_0
       19:  getfield        #3; //Field field:Ljava/lang/Object;
       22:  if_acmpne       29
       25:  iconst_1
       26:  goto    30
       29:  iconst_0
       30:  ireturn
      LineNumberTable:
       line 6: 0
    }

  • Hey! something you may not know .....

    a  Laptop Battery is portable and can run on batteries alone.
    Nickel-Cadmium (NiCad) batteries were the first type of battery commonly used in laptop computers, and older laptops sometimes still use them. They have a life of roughly two hours between charges, but this life decreases with each charge based on thememory effect. Gas bubbles form in the cell plates, reducing the total amount of available cell space for recharge. The only way around this is to discharge the battery completely before recharging it. The other drawback of NiCad is that if the battery charges too long, it can explode.
    Nickel-Metal Hydride (NiMH) batteries are the bridge between NiCad and the newer Lithium-Ion (LiIon) batteries. They last longer between charges than NiCad but overall have a shorter total lifespan. They suffer from the memory effect, but to a lesser extent than NiCad batteries.
    LiIon batteries are the current standard for laptop computers. They are light and have long life spans. They do not suffer from the memory effect, can be charged randomly, and won't overheat if overcharged. They are also thinner than any other battery available for laptops, making them ideal for the new ultra-thin notebooks. LiIon batteries can last for anything from about 950 up to 1200 charges.
    Many laptops with LiIon batteries claim to have a 5-hour battery life, but this measurement can vary greatly depending on how the computer is used. The hard drive, other disk drives and LCD display all use substantial battery power. Even maintaining wireless Internet connectivity requires some battery power. Many laptop computer models have power management software to extend the battery life or conserve battery power when the battery is low.

    Hmm, made some elementary mistakes there.
    What I should have said was that the declared primitive type of the Java constant is converted to its C/C++ equivalent so..
    Java int == C/C++ long
    Java short == C/C++ short
    Java byte == C/C++ byte
    I would assume that Java String literals are converted to #define literals, with the L"literal" prefix so as to ensure UNICODE. I don't use String constants in this manner for my current project so I leave it up to you as an exersize to find out what happens.
    To correct my previous post then the first example should have read..
    package com.domain.package;
    public class AClass {
    public static final int A_CONSTANT 12;
    javah will transform these constants into #defines whose names follow the name mangling conventions for classes run through javah.
    The constant above is transformed into..
    #undef com_domain_package_AClass_A_CONSTANT
    #define com_domain_package_AClass_A_CONSTANT 12L
    The second example you can ignore

  • EDitorKit development -- XMLEditorKit

    Does anyone know if future versions of Java will include a XMLEditorKit, or is something like this available elsewhere?
    I am attempting to build a simple text editor capable of highlighting XML tags, elements, attributes, etc. with different colors to ease XML document creation. I know there are existing editors like this on the market (I use JBuilder5, which has this built in.) But I want to be able to make my own editor so that I can include features custom for a specific projects, such as including the same validator engine used in our production, and ability to "publish" completed documents to our own server.
    I have studied the JavaEditorKit example at the following URL: http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html
    However, the above example uses several undocumented classes. Specifically, I find it difficult to follow what they have done in the sample Token class because they are using the sun.tools.java.Constants class, which I cannot seem to figure out without any documentation.
    I have seen other posts in this forum on this subject, but nothing very recent. Just wondering if anyone out there has any EditorKit "news" or updated tutorials, or has any sample code they would be willing to share.
    Thanks in advance.

    I recently unjar'ed (i.e. unzipped) tools.jar, ant then used the program javap to look at the signature og each method.
    If you want the whole source you can decompile the class file using cavaj or jad (java de-compiler) http://www.bysoft.se/sureshot/cavaj/

  • String with length 1 became 4 when nls_lang_semantics=CHAR

    Hi,
    We are using Oracle 9.2.0.1 database with charset = UTF-8, and we have set nls_lang_semantics=CHAR.
    We got a problem in our java program is that for a column with char(1), after I insert a java constant 'Y', the result I get back from select is a string of 'Y ', i.e. the string returned is not a string with length=1, but with length=4!! How can I correct the problem to get a string with length=1?
    Regards,
    Roy

    Hi,
    Thanks for the responses but I don't think you're quite right. Forms 6i should indeed work with Unicode as a database character set as long as it is UTF8 rather than AL32UTF8. This can be seen in various Metalink documents and also in the Release 6i Guidelines for Building Applications here (section 4.1.5):
    http://download-west.oracle.com/otn_hosted_doc/forms/forms/A73073_01.pdf
    The problems started when I changed NLS_LENGTH_SEMANTICS to CHAR. Its understandable if Forms 6i does not recognise this setting as it pre-dates it but I wanted to confirm this.
    We want to migrate our existing application to work with multibyte characters. Unfortunately there are table columns, PL/SQL variables etc. that are too small when defined as a length in bytes.
    I am looking for the least impact method to rectify this. On the server I can use NLS_LENGTH_SEMANTICS but if I can't get Forms to work with it then I may need to rethink.
    I realise that using Forms 9i or 10g would support the parameter but it is not possible to upgrade at the moment.
    Has anyone any other suggestions or methods for migrating an application to using multibyte characters?
    Thanks again,
    Kevin.

Maybe you are looking for

  • My MacBook Pro internal speakers sound like they are playing through the headphone jack, why is this? How can I fix it?

    Periodically, the sound from my MacBook Pro speakers cuts out to where it sounds as if I am just blasting music through a pair of headphones.  The start up sounds and even some normal sounds coming from my computer are at the regular balanced speaker

  • Error by opening the Central configuration manager

    Hi Gurus, i have a problem with my system. I installed Enteprise XI 3.0, Crystal, CR, Xcelsius. By trying to install Live office i receive an error. I open the Central configuration manager and received this:" The procedure entry pint? Can stack Dump

  • Released from iphone contract

    I purchased an iphone in November and it was activated onto an iphone tariff. After months of billing/payment problems o2 have released me from my contract 13 months early so that i'm no longer in contract with them. I'm puzzled as to what to do with

  • Unable to transfer i=tunes to new computer

    I have tried to transfer my i-tunes library to my new computer. I have been ollowing the instructions on the I-Tunes site, which tells me to connect the i-pod to the new computer and to clickon the i-pod device and then drag the i-tunes folder to the

  • Kernel 2.6.32-ARCH slows down and syslog-ng goes crazy *

    Hi, it's a month now that randomly my computer slows down. I took a look with "iotop" and found that syslog-ng was going crazy writing continuosly to the logfile. taking a look at these files i found this message that gets written over and over again