Results 1 to 4 of 4
  1. #1
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default constant variables questions

    I have this final final short named 40 and its always constant no matter what. Is there a way so ALL of the classes share this variable?Because currently i have to put final short blah = 40; at the beginning of all my dam classes and its making my code so messy. Help!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can declare it in a class as public static final short = final. Some other things you can think of is the PI constant supplied by the class Math.

    It is declared as
    Java Code:
    public static final double PI = 3.141592....
    If you add that short to a class you can import the package and use it as
    Java Code:
    ClassName.CONTANT_NAME

  3. #3
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default

    how to import?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You have to package your class with the constant first, than import the class.

    Using Package Members (The Java™ Tutorials > Learning the Java Language > Packages)

    Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)


    Basically, you create a class and make the first line
    Java Code:
    package directoryName
    As an example, I have written a class which is in the texteditor/actions directory, and the first line of all the files in this folder is
    Java Code:
    package texteditor.actions
    Then I have some other classes which rely on the classes in the directory texteditor/actions. I gain access to them in other files with this line
    Java Code:
    import texteditor.actions.*;
    You will have to package a class with the constants and then import it when necessary.

Similar Threads

  1. Replies: 6
    Last Post: 01-26-2011, 08:49 PM
  2. Replies: 3
    Last Post: 01-03-2011, 04:48 PM
  3. switch() don't work with constant
    By jarek in forum Advanced Java
    Replies: 3
    Last Post: 11-19-2010, 09:59 AM
  4. static constant and performance
    By felixtfelix in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 02:43 PM
  5. Constant size for JTextArea
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2008, 05:12 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
  •