11 Replies Latest reply on Mar 5, 2009 7:41 PM by 807588

    Collection available to all the classes

    807588
      Hello,

      I am just wondering if I can have a collection of an object and that collection is made available to all the classes rather than creating that collection again and again. For example I am deserializing an object from the file in one of the class, how can I make that object available to all the classes, I don’t want to read it again and again. This can be done easily when using java servlet, but my application is standalone, so is there any way of achieving this?

      Any idea will be appreciated.

      Many Thanks,
        • 1. Re: Collection available to all the classes
          577611
          Make it public static.
          • 2. Re: Collection available to all the classes
            807588
            Sorry make what public static?

            Say i have got three classes:
            Public class Word(){
            
            private String word;
            private String pos;
            
            public Word(String word,String pos ){
            
            this.word = word;
            this.pos=pos;
            }
            }
            HashSet<Word> w = new HashSet<Word>();
            Word w = new Word("run","verb");
            w.add(w);
            how can i make collection w to be available to every other class in the project?

            Edited by: veer2go on Mar 5, 2009 3:44 AM

            Edited by: veer2go on Mar 5, 2009 3:45 AM
            • 3. Re: Collection available to all the classes
              577611
              Collection "w" should be public static. This must be declared at the class level.
              public static HashSet<Word> w = new HashSet<Word>();
              .
              • 4. Re: Collection available to all the classes
                807588
                Yeah Thanks mate, do u think, this approach is better or passing the collection through the parameters is better?
                • 5. Re: Collection available to all the classes
                  JoachimSauer
                  veer2go wrote:
                  Yeah Thanks mate, do u think, this approach is better or passing the collection through the parameters is better?
                  That question is opening a huge can of worms.

                  Normally I'd say avoid the static version as much as possible. Pass the Set to all objects that need it.

                  This could lead to the Set being passed through many levels of calls, 'though. This usually means that your classes are not isolated enough and need to be refactored.

                  So both solutions can become quite ugly and writing a clean one takes some experience.
                  • 6. Re: Collection available to all the classes
                    807588
                    JoachimSauer wrote:
                    veer2go wrote:
                    Yeah Thanks mate, do u think, this approach is better or passing the collection through the parameters is better?
                    That question is opening a huge can of worms.

                    Normally I'd say avoid the static version as much as possible. Pass the Set to all objects that need it.

                    This could lead to the Set being passed through many levels of calls, 'though. This usually means that your classes are not isolated enough and need to be refactored.

                    So both solutions can become quite ugly and writing a clean one takes some experience.
                    +1

                    The assumption that all classes need to see this collection is to be challenged. It's usually made in order to avoid working out exactly what needs the object in question
                    • 7. Re: Collection available to all the classes
                      577611
                      Declaring it as static is simpler. That's all.

                      Also remember that passing the Set object to methods does not mean you are creating copies of it.
                      • 8. Re: Collection available to all the classes
                        807588
                        I have got set of GUI's say about 2, so i am abit confused, what to use? i like the idea of public static, but then gain it might lead to some problem. At the moment i am just passing the object through the parameters, but i think for that i had to create some extra methods.

                        Edited by: veer2go on Mar 5, 2009 4:19 AM
                        • 9. Re: Collection available to all the classes
                          JoachimSauer
                          veer2go wrote:
                          I have got set of GUI's say about 2, so i am abit confused, what to use? i like the idea of public static, but then gain it might lead to some problem. At the moment i am just passing the object through the parameters, but i think for that i had to create some extra methods.
                          If you've got two UI components that show the same data, then create a model object that contains all the data that you want to display. In your case that could just be the Set. Then pass around that model.

                          Advantages over just passing around the Set:
                          * The type tells you what the content is to be used for
                          * You can add more fields/change the implementation later on if necessary
                          * You can add higher-level methods to modify the model.
                          • 10. Re: Collection available to all the classes
                            807588
                            rksharma wrote:
                            Declaring it as static is simpler. That's all.
                            Simpler than what?
                            • 11. Re: Collection available to all the classes
                              807588
                              georgemc wrote:
                              rksharma wrote:
                              Declaring it as static is simpler. That's all.
                              Simpler than what?
                              Well, simple-minded.