9 Replies Latest reply: Oct 21, 2008 3:04 PM by 843785 RSS

    Help ! Problem with static context"

    843785
      What does it means ? "non-static variable this cannot be referenced from a static context"
      Error on NEW...

      import java.util.*;

      public class Test {
      public static void main (String[] args) {
      Reponse r = new Reponse ();
      r.demander ();
      System.out.println (r.getBP () + " " + r.getMP ());
      }

      class Reponse {
      public void demander () {
      ...
      }
      public int getBP () {
      return (nbBP);
      }
      public int getMP () {
      return (nbMP);
      }
      private int nbBP, nbMP;
      }
      }
        • 1. Re: Help ! Problem with static context"
          843785
          It means you can't call non static methods/variables from static block
          public static void main (String[] args) {
          Reponse.demander();
          }
           class Reponse {
           public static void demander () {
           ...
           }
          • 2. Re: Help ! Problem with static context"
            843785
            Why is your Reponse class not public?
            • 3. Re: Help ! Problem with static context"
              843785
              phdk wrote:
              Why is your Reponse class not public?
              Because that would cause a compiler error?
              • 4. Re: Help ! Problem with static context"
                843785
                tsith wrote:
                phdk wrote:
                Why is your Reponse class not public?
                Because that would cause a compiler error?
                Thanks tsith!
                Maybe I misunderstand your reply.
                I am sure you had no idea what I meant. I'll try to make it clear.
                Is there any reason why it is defined as an inner class?
                • 5. Re: Help ! Problem with static context"
                  843785
                  phdk wrote:
                  tsith wrote:
                  phdk wrote:
                  Why is your Reponse class not public?
                  Because that would cause a compiler error?
                  Thanks tsith!
                  Maybe I misunderstand your reply.
                  I am sure you had no idea what I meant. I'll try to make it clear.
                  Is there any reason why it is defined as an inner class?
                  It's not an inner class, it's a top-level class, hence tsith's response
                  • 6. Re: Help ! Problem with static context"
                    843785
                    georgemc wrote:
                    phdk wrote:
                    tsith wrote:
                    phdk wrote:
                    Why is your Reponse class not public?
                    Because that would cause a compiler error?
                    Thanks tsith!
                    Maybe I misunderstand your reply.
                    I am sure you had no idea what I meant. I'll try to make it clear.
                    Is there any reason why it is defined as an inner class?
                    It's not an inner class, it's a top-level class, hence tsith's response
                    Actually, it is defined as an inner class, but the lack of code formatting makes this hard to see:
                    import java.util.*;
                    
                    public class Test {
                        public static void main (String[] args) {
                            Reponse r = new Reponse ();
                            r.demander ();
                            System.out.println (r.getBP () + " " + r.getMP ());
                        }
                    
                        class Reponse {
                            public void demander () {
                            }
                    
                            public int getBP () {
                                return (nbBP);
                            }
                    
                            public int getMP () {
                                return (nbMP);
                            }
                    
                            private int nbBP, nbMP;
                        }
                    }
                    Why an inner class? My guess is that the OP doesn't know what he is doing.
                    • 7. Re: Help ! Problem with static context"
                      baftos
                      If you check the brackets with a magnifier, it is an inner class.
                      This is not important.
                      @OP - I feel that you are too novice in Java to place two classes in the same file (inner or top level).
                      This will cause all kind of problems that will just obscure your learning, for now.
                      Place each class in a separate file and see what happens.

                      Also learn how to use the code tags (there is a CODE button that you should use when you compose your post).
                      We will see much better where the brackets are.
                      • 8. Re: Help ! Problem with static context"
                        843785
                        BigDaddyLoveHandles wrote:
                        georgemc wrote:
                        phdk wrote:
                        tsith wrote:
                        phdk wrote:
                        Why is your Reponse class not public?
                        Because that would cause a compiler error?
                        Thanks tsith!
                        Maybe I misunderstand your reply.
                        I am sure you had no idea what I meant. I'll try to make it clear.
                        Is there any reason why it is defined as an inner class?
                        It's not an inner class, it's a top-level class, hence tsith's response
                        Actually, it is defined as an inner class, but the lack of code formatting makes this hard to see:
                        import java.util.*;
                        
                        public class Test {
                        public static void main (String[] args) {
                        Reponse r = new Reponse ();
                        r.demander ();
                        System.out.println (r.getBP () + " " + r.getMP ());
                        }
                        
                        class Reponse {
                        public void demander () {
                        }
                        
                        public int getBP () {
                        return (nbBP);
                        }
                        
                        public int getMP () {
                        return (nbMP);
                        }
                        
                        private int nbBP, nbMP;
                        }
                        }
                        Why an inner class? My guess is that the OP doesn't know what he is doing.
                        Well, thanks BDLH! My point exactly!
                        • 9. Re: Help ! Problem with static context"
                          843785
                          BigDaddyLoveHandles wrote:
                          georgemc wrote:
                          phdk wrote:
                          tsith wrote:
                          phdk wrote:
                          Why is your Reponse class not public?
                          Because that would cause a compiler error?
                          Thanks tsith!
                          Maybe I misunderstand your reply.
                          I am sure you had no idea what I meant. I'll try to make it clear.
                          Is there any reason why it is defined as an inner class?
                          It's not an inner class, it's a top-level class, hence tsith's response
                          Actually, it is defined as an inner class, but the lack of code formatting makes this hard to see:
                          Ah - totally missed that, apologies.