8 Replies Latest reply: Feb 26, 2008 8:22 AM by 807601 RSS

    subclass superclass

    807601
      Hello All,

      According to my java knowhow this should would:

      package bts;
      class Super {
          String pietje = new String("pietje");
      }
      class Sub extends Super {
          public static void main(String args[]){
          
              System.out.println(super.pietje);
          }
          
      }
      but it doesn't? It tells me:

      Documents\NetBeansProjects\bts\src\bts\kleintje.java:9: non-static variable super cannot be referenced from a static context

      Why is this? This is normal inheritance isn;t it?

      Thanks!
        • 1. Re: subclass superclass
          791266
          tnk2k wrote:
          Hello All,

          According to my java knowhow this should would:

          package bts;
          class Super {
          String pietje = new String("pietje");
          }
          class Sub extends Super {
          public static void main(String args[]){
          
          System.out.println(super.pietje);
          }
          
          }
          but it doesn't? It tells me:

          Documents\NetBeansProjects\bts\src\bts\kleintje.java:9: non-static variable super cannot be referenced from a static context

          Why is this? This is normal inheritance isn;t it?

          Thanks!
          Your question isn't related to inheritance. The problem is that pietje isn't static, it belongs to an instance but you are trying to access it as if it was static. You would get the same error message if you moved the declaration to Sub.

          Kaj
          • 2. Re: subclass superclass
            807601
            class Super {
                String pietje = new String("pietje");
            }
            class Sub extends Super {
                public static void main(String args[]){
                
                    System.out.println(super.pietje);
                }
                
            }
            First, if you extend any class you need to initiate it from the sub class, without initiating it in subclass constructor you wont be able to call it. And sencond thing the main class is static, you can never make a call in a static class. You need to refer some basics
            • 3. Re: subclass superclass
              800308
              treky, Again you really DO need to RTFM... or not, it's completely up to you.

              Going through the http://java.sun.com/docs/books/tutorial/ will get you a basic grounding a lot less painfully than givng yourdself the unguided tour of java.

              TIJ3 was also really good, and an excellent price... Free.
              • 4. Re: subclass superclass
                791266
                First, if you extend any class you need to initiate it from the sub class, without initiating it in subclass constructor you wont be able to call it.
                O_o

                What are you saying? This doesn't sound correct.

                Kaj
                • 5. Re: subclass superclass
                  807601
                  As far as i know you should call a super() class inside the constructor for subclass, if you do not then java will automatically put it for you. And since main is always static you cannot make a call outside the class from main.

                  Let me know, if thts right.
                  • 6. Re: subclass superclass
                    791266
                    rizncsu wrote:
                    As far as i know you should call a super() class inside the constructor for subclass, if you do not then java will automatically put it for you. And since main is always static you cannot make a call outside the class from main.

                    Let me know, if thts right.
                    My objection was to the statement that I quoted.

                    "First, if you extend any class you need to initiate it from the sub class, without initiating it in subclass constructor you wont be able to call it."

                    You don't need to have a constructor, and as you said, a call to the super constructor is inserted if the base class has a default constructor, so you usually don't have to do anything.

                    It sounded like you said that you must do something in your constructor if you are extending another class.

                    Kaj
                    • 7. Re: subclass superclass
                      807601
                      class Super {
                      String pietje = new String("pietje");
                      }
                      class Sub extends Super {
                      public static void main(String args[]){

                      System.out.println(super.pietje);
                      }

                      }

                      hi friend, first you have to create object for the class "Super" in "Sub" class

                      class Super {
                      String pietje = new String("pietje");
                      }
                      class Sub extends Super {
                      public static void main(String args[]){
                      Super super=new Super();
                      System.out.println(super.pietje);
                      }

                      }

                      this piece of code will work fine or mark variable "pietje" in "Super" class as static.
                      • 8. Re: subclass superclass
                        807601
                        Ramachandran_KN wrote:
                        this piece of code...
                        What you posted couldn't have been code, since "code" goes inside of code blocks.
                        System.out.println("Like this");