4 Replies Latest reply on Mar 18, 2009 8:46 AM by nimo22

    Is static implicitly Out In

    nimo22

      I need this static:



      @Out(required=false) private MyObject object;




      Should I use a normal static getter/setter or does Seam do it automatically static?



      private static MyObject object;
      public static ...get
      public static void set...


        • 1. Re: Is static implicitly Out In
          nimo22

          Why I have to use this:


          @In(required=false) @Out(required=false) private MyObject myObject = new MyObject();




          to avoid a NPE of myObject ?


          I have thought, that


          @In(required=false)



          creates a new Instance of myObject automatically, but this is not the case. I have to use the new Operator (which I have learned should avoid when using @In, @Out)

          • 2. Re: Is static implicitly Out In
            kragoth

            Logically it makes no sense to say required=false if you actually want an instance to be created.


            By the way, you should never use the new operator on any seam bean. regardless of whether you inject it or not. If you use the new operator you are completly bypassing the SEAM infrastructure and none of SEAM code in that bean will work. I don't want to go in to the details of why this doesn't work, but essentially SEAM beans need to be managed by SEAM. So when you use the new operator you are not allowing SEAM to manage the bean.


            If you have:


            @In(required=false)
            



            Then this means, inject the variable if it exists, otherwise leave the value set to null.


            If you want an instance to always exist you should do:


            @In(required=true, create=true)
            



            This means, inject the variable if it exists, otherwise inject a new instance.



            Also why do you want the variable static? Do you understand what you are actually doing when you do a static variable?


            To make a long answer short, @In @Out is not the same as 'static'.


            If you want a global variable maybe you just need an Application scoped bean.

            • 3. Re: Is static implicitly Out In
              nimo22

              So doing static in a ApplicationScoped-Class:



              @Name("app")
              @Scope(APPLICATION)
              @Startup
              public class App implements HttpSessionListener {
              
              // Out is not regocnized due to the new-Operator???
              @Out(required=true) private static MyObject myObject = new MyObject ();
              
              // Out is not regocnized due to No new-Operator
              @Out(required=true, create=true) private static MyObject myObject;
              
              ...}




              So you see, normally myObject is implititly static due to the application-scope, so I can leave the static-keyword. Am I right?



              By the way, you should never use the new operator on any seam bean.

              So when I want to refresh a (seam-)variable elsewhere in the code, I should not use the new Operator?:



              public void init(){
              
              // forbidden??
              myObject = new MyObject ()
              }


              • 4. Re: Is static implicitly Out In
                nimo22

                This instance:



                // Out is not regocnized due to No new-Operator
                @In(required=true, create=true) @Out private static MyObject myObject;





                is not available in Listeners, e.g.



                public void sessionCreated(HttpSessionEvent sessionEvent)
                     {
                //causes a NPE !!
                System.out.print(myObject);
                
                }