Results 1 to 6 of 6
  1. #1
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default How to set static object?

    I wish to ask for the best practice to set and use a static object.

    let's say packageA has a set of names. packageB needs to use that set of names.

    What I have in mind is in some (library) class in package A, define this set of names and pass that to packageB.

    say:
    sNames = new TreeSet<String>();
    sNames.add( "Name1" );
    sNames.add( "Name2" );
    sNames.add( "Name3" );
    sNames.add( "Name4" );
    sNames.add( "Name5" );

    it sounds to me that ideally this object should be static and final, so that in packageB i can call it by importing packageA and do Set<String> s = AAA.sNames;

    Can I instantiate a set/object with initial values like what I do for
    public static final int N = 100?

    If this is not a viable approach, what is the best practice to achieve my purpose?

    However, I don't know how to set the value of a static object..

    Thanks in advance for any ideas and insights!!

  2. #2
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: How to set static object?

    to make my question clearer, one dumb way is that in any class of packagaA,

    I define the set as a instance variable, say under object class ABC.java

    every time I instantiate an object of ABC, I initialize the set, so that ABC carries this set and can be retrieved using a get method.

    However it makes no sense to instantiate another object to retrieve a set is it?

    Or i can define this set alone as an object, which is then sort of constant in packageA.

    But that sounds a bad idea too.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: How to set static object?

    You can use a static initializer block. It's sort of like a constructor for the class itself, rather than for an instance of it. It runs when the class is loaded, and it looks like this:

    Java Code:
    public class MyClass {
        public static final Set<String> sNames;
    
        static {
             sNames = new TreeSet<String>();
             // add elements
        }
    }
    Keep in mind that the reference to the Set is final, but elements could still be added or removed from it. You may want to make it private and provide a getter that returns a copy.
    Last edited by kjkrum; 04-17-2012 at 02:09 AM. Reason: edited to better reflect what you're asking
    Get in the habit of using standard Java naming conventions!

  4. #4
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: How to set static object?

    Thanks kjkrum for the reply! I am trying that out right now!

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

    Default Re: How to set static object?

    Or return an unmodifiable view of the set, or maybe define an enum if these names are names of some enumerable set of things.

    Using enum rather clearly expresses the fact that these names relate to a type of thing, rather than just being some static data bolted on to a class. As such they can be the subject of switch statements for instance.

  6. #6
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: How to set static object?

    Thanks pbrockway2 for the excellent suggestion!

    did more look up after my trial using kjkrum's method (which works!!) and seems the following is exactly what I was looking for..
    Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Similar Threads

  1. Object data overlapping.- Need help with static variables.
    By Anerxomounos in forum New To Java
    Replies: 3
    Last Post: 05-27-2011, 07:47 PM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. Replies: 3
    Last Post: 03-24-2010, 12:24 AM
  4. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  5. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 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
  •