Same Method name

Discussion in 'Java' started by durgaapparaobsc@gmail.com, Mar 14, 2007.

  1. Guest

    Hai.......users.....


    This is appu........

    i learning to the java ........i want to a small dought

    can we create same name of static method and instance method

    with the same class


    with regards

    appu
    , Mar 14, 2007
    #1
    1. Advertising

  2. ck Guest

    On Mar 14, 11:37 am, wrote:
    > Hai.......users.....
    >
    > This is appu........
    >
    > i learning to the java ........i want to a small dought
    >
    > can we create same name of static method and instance method
    >
    > with the same class
    >
    > with regards
    >
    > appu


    Yes
    --
    Ck
    http://www.gfour.net
    ck, Mar 14, 2007
    #2
    1. Advertising

  3. Guest

    hello sir,

    this is appu....

    if dont mind write that code else i write to here any modification to
    correct the stuff

    class SameName {

    //Constroctor
    SameName() {
    this.dis();
    }

    //instance method
    void dis() {
    S.o.p("This is Instance method");
    }

    static void dis() {
    S.o.p("This is Static method");
    }

    class Demo {
    public static void main(String a[]) {
    SameName sn = new SameName();
    sn.dis();
    SameName.dis();
    }
    }



    In the above stuff comes to the error plz send the mail to with
    modifications..........

    with regards
    Appu
    , Mar 14, 2007
    #3
  4. ck Guest

    On Mar 14, 12:08 pm, wrote:
    > hello sir,
    >
    > this is appu....
    >
    > if dont mind write that code else i write to here any modification to
    > correct the stuff
    >
    > class SameName {
    >
    > //Constroctor
    > SameName() {
    > this.dis();
    >
    > }
    >
    > //instance method
    > void dis() {
    > S.o.p("This is Instance method");
    >
    > }
    >
    > static void dis() {
    > S.o.p("This is Static method");
    >
    > }
    >
    > class Demo {
    > public static void main(String a[]) {
    > SameName sn = new SameName();
    > sn.dis();
    > SameName.dis();
    >
    > }
    > }
    >
    > In the above stuff comes to the error plz send the mail to with
    > modifications..........
    >
    > with regards
    > Appu


    method name can be same but method signature have to be different to
    coexist.

    Change one of the dis by adding parameter to it.
    Eg:-
    void dis() {
    // do something
    }

    static void dis(String parm){
    // do something else
    }

    --
    Ck
    http://www.gfour.net
    ck, Mar 14, 2007
    #4
  5. Lew Guest

    wrote:
    >> hello sir,
    >> class SameName {
    >>
    >> //Constroctor
    >> SameName() {
    >> this.dis();
    >>
    >> }
    >>
    >> //instance method
    >> void dis() {
    >> S.o.p("This is Instance method");


    What is 'S.o.p'? I see no definition.

    Please provide SSCCEs.

    >>
    >> }
    >>
    >> static void dis() {
    >> S.o.p("This is Static method");


    Consider indenting code by a few spaces as suggested by the Sun Java coding
    conventions (Sun, 1999) and nearly universally practiced.

    >> }
    >>
    >> class Demo {
    >> public static void main(String a[]) {
    >> SameName sn = new SameName();
    >> sn.dis();
    >> SameName.dis();
    >>
    >> }
    >> }


    ck answered your main question. The compiler resolves method signatures first,
    'static' and other qualifiers after. Two methods with the same name in the
    same class must have different signatures (overloading).

    -- Lew
    Lew, Mar 14, 2007
    #5
  6. PÃ¥ Wed, 14 Mar 2007 07:37:27 +0100, skrev <>:

    > can we create same name of static method and instance method


    Methods have to be different in their signatures; the signature is made up
    of the case-sensitive name and the parameter types, in order. Since
    neither return type or modifiers like static form part of the signature,
    you cannot have a static and a non-static method with the same name and
    parameter list, nor two methods with the same name and parameter list that
    returns different things.
    Tor Iver Wilhelmsen, Mar 22, 2007
    #6
  7. Tor Iver Wilhelmsen wrote:
    > På Wed, 14 Mar 2007 07:37:27 +0100, skrev <>:
    >
    >> can we create same name of static method and instance method

    >
    > Methods have to be different in their signatures; the signature is
    > made up of the case-sensitive name and the parameter types, in order.
    > Since neither return type or modifiers like static form part of the
    > signature, you cannot have a static and a non-static method with the
    > same name and parameter list, nor two methods with the same name and
    > parameter list that returns different things.


    Nor, I think, methods that differ only in whether (or how) an argument is
    genericized, e.g. not both

    void method(List l)

    and

    void method(List<T> l).
    Mike Schilling, Mar 23, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,786
    ding feng
    Jun 25, 2003
  2. Replies:
    3
    Views:
    478
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    May 16, 2006
  3. Replies:
    5
    Views:
    331
    Alf P. Steinbach
    Feb 6, 2006
  4. QQ
    Replies:
    6
    Views:
    447
    Roedy Green
    Mar 28, 2008
  5. rodchar

    mvc same name same post

    rodchar, Oct 8, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    280
    rodchar
    Oct 8, 2009
Loading...

Share This Page