CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    1

    Pass by value or reference



    HI. I am a C++ programmer mingrating to Java



    I have this query..Does Java support pass by value or pass by reference.

    Following code suggests that Java supports Pass by reference as it calls the "callme " function from the derieved

    class. But then it calls the constructor of the base class also which suggests that it makes a copy of base class...It

    means its pass by value..

    Please help


    class Base {

    Base(){

    System.out.println("I am in base class constructor "

    }

    void callme(){

    System.out.println("I am in base class"

    }



    }

    class Derieved extends Base {

    void callme(){

    System.out.println("I am in derieved class"

    }

    }


    class dispatch {

    public static void main(String args[]) {

    Derieved dd = new Derieved() ;

    fff(dd);

    }

    }



    static void fff(Base bb){

    bb.callme();

    }

    }




    OUTPUT is

    I am in base class constuctor

    I am in base derieved class





  2. #2
    Join Date
    May 1999
    Posts
    93

    Re: Pass by value or reference



    Java (almost) always uses pass by reference. The only time it uses pass by

    value is for int, boolean, char etc. Note that in Java all access to an object

    is through its reference.


    The first line of output you get is when the Derived class is constructed. As in

    C++, the subclass construction continues only after the base class portion gets

    constructed.


    The second line of output of course is from the callme() of the Derived class.

  3. #3
    Join Date
    Feb 2001
    Location
    india
    Posts
    2

    Re: Pass by value or reference

    Mr. Manoj

    the out put is
    I am in base class constructor
    "I am in base class"

    b'coz u r calling the method of the base class using it's object obviously the result is as above and the constructor is calls by default when ever u create ad nobject of the class



    vinay kumar s.v

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured