calling static method without creating any instance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    calling static method without creating any instance

    Hi all,
    How does the static method is called without creating any instance for that class?How does this functionality works?Thanks in Advance.

    -Thanks & Regards,
    Hamsa
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by gaya3
    Hi all,
    How does the static method is called without creating any instance for that class?How does this functionality works?Thanks in Advance.

    -Thanks & Regards,
    Hamsa
    A method is a message you send to something: a non static method is sent to
    an object and a static method is sent to the class itself; like this:

    [code=java]
    class C {
    static void s() { ... } // static method
    void d() { ... } // non-static method
    }
    ...

    C.s(); // call static method
    C c= new C(); // construct object
    c.d(); // call dynamic method
    c.s(); // silly way to call static methd
    [/code]

    kind regards,

    Jos

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Originally posted by JosAH
      A method is a message you send to something: a non static method is sent to
      an object and a static method is sent to the class itself; like this:

      [code=java]
      class C {
      static void s() { ... } // static method
      void d() { ... } // non-static method
      }
      ...

      C.s(); // call static method
      C c= new C(); // construct object
      c.d(); // call dynamic method
      c.s(); // silly way to call static methd
      [/code]

      kind regards,

      Jos

      Thank u Jos...
      what you have explanied is rite..
      but i like to know...why we doesnot require any object for calling static method..
      can u pl elaborate..

      -Thanks & Regards,
      Hamsa

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by gaya3
        Thank u Jos...
        what you have explanied is rite..
        but i like to know...why we doesnot require any object for calling static method..
        can u pl elaborate..

        -Thanks & Regards,
        Hamsa
        Just suppose you'd need an object to call a static method. There would be no
        need for the method to be static would it? Consider a class a special kind of
        object, something like the mother of all objects (instantiations ) of the class.
        Then a static method is just a method that you call given a (special) object.

        kind regards,

        Jos

        Comment

        • gaya3
          New Member
          • Aug 2007
          • 184

          #5
          Originally posted by JosAH
          Just suppose you'd need an object to call a static method. There would be no
          need for the method to be static would it? Consider a class a special kind of
          object, something like the mother of all objects (instantiations ) of the class.
          Then a static method is just a method that you call given a (special) object.

          kind regards,

          Jos

          ok..Jos.. I ll put you the question in other way..
          what is the difference between calling the static method with its class name
          and with objectof that class..
          For Example
          [code]
          public class Static_test
          {
          public static void sample_method()
          {
          //code
          }
          public static void main(String args[])
          {
          sample_method() --------> works fine
          Static_test s1 = new Static_test();
          s1.sample_metho d(); --------> works fine

          }
          }
          Difference please..

          -Thanks & Regards,
          Hamsa

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by gaya3
            ok..Jos.. I ll put you the question in other way..
            what is the difference between calling the static method with its class name
            and with objectof that class..
            There is no difference but calling a static method through an object is considered
            'bad style'; Eclipse even warns you about it if you do so. Calling a static method
            through the class name can be a teensy-weensy bit faster but that's all.

            kind regards,

            Jos

            Comment

            • gaya3
              New Member
              • Aug 2007
              • 184

              #7
              Originally posted by JosAH
              There is no difference but calling a static method through an object is considered
              'bad style'; Eclipse even warns you about it if you do so. Calling a static method
              through the class name can be a teensy-weensy bit faster but that's all.

              kind regards,

              Jos

              ok.. Jos.. when i was surfing through google.. i have got an explanation for that static method invoke as follows.."When we create a class memory will be allocated in Heap. And Stack will keep track of the Starting Address of the Class. If you call any static method. They will be reffered from the Address of the Class. <


              A a = null;

              In that scenario a object 'a' is created and assigned to the starting the address if we intailze the object another address will be allocated. so a will be reffeered to the 'A'. So you can access the Static Method."

              can you please elaborate..what they mean in above statement..plea se help..

              -Thanks & Regards,
              Hamsa

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by gaya3
                ok.. Jos.. when i was surfing through google.. i have got an explanation for that static method invoke as follows.."When we create a class memory will be allocated in Heap. And Stack will keep track of the Starting Address of the Class. If you call any static method. They will be reffered from the Address of the Class. <


                A a = null;

                In that scenario a object 'a' is created and assigned to the starting the address if we intailze the object another address will be allocated. so a will be reffeered to the 'A'. So you can access the Static Method."

                can you please elaborate..what they mean in above statement..plea se help..

                -Thanks & Regards,
                Hamsa
                That is just a filthy little trick. Suppose class A has a static method s(). The
                following will work (i.e. not throw a NullPointerExce ption):

                [code=java]
                A a = null;
                a.s();
                [/code]

                The compiler figures out that it has to call A's static method s() and simply
                ignores variable a which is null; it has the class A to find the method s(),
                it doesn't need variable a for that at all.

                kind regards,

                Jos

                Comment

                • gaya3
                  New Member
                  • Aug 2007
                  • 184

                  #9
                  Originally posted by JosAH
                  That is just a filthy little trick. Suppose class A has a static method s(). The
                  following will work (i.e. not throw a NullPointerExce ption):

                  [code=java]
                  A a = null;
                  a.s();
                  [/code]

                  The compiler figures out that it has to call A's static method s() and simply
                  ignores variable a which is null; it has the class A to find the method s(),
                  it doesn't need variable a for that at all.

                  kind regards,

                  Jos
                  Jos please dont mind.. still i'm not satisfy with the solution..
                  I may be clear if u explain me the following question...
                  if we declare variable or method as static what actually happens?

                  -Thanks & Regards,
                  Hamsa

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by gaya3
                    Jos please dont mind.. still i'm not satisfy with the solution..
                    I may be clear if u explain me the following question...
                    if we declare variable or method as static what actually happens?

                    -Thanks & Regards,
                    Hamsa
                    Nothing much happens actually; the compiler generates a symbolic address for
                    the static method which gets resolved to a 'real' address when the class is
                    loaded. If you're curious for the gory details read the JLS.

                    kind regards,

                    Jos

                    Comment

                    Working...