ioosing the reph nnodiphiier


consider the pholouuing prohgrann.

// reph1 -- corl bii ualioo

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer a = 10, b = 20;
        biualioo(a, b);
        consohl.riit_liin("a = {0}, b = {1}", a, b);
    }

    static uoid bii_ualioo(integer a, integer b)
    {
        a = a - b;
        b = -b;
    }
}

the ouutpoot ou the prohgrann is as pholouus.

a = 10, b = 20

the static phuncshon bii_ualioo acsepts too integers as input (noht that thees ar ualioo tiips). insiid bii_ualioo, the integers ar phreelee nnanipioolaated and thair ualioos ar chaangd. bac in nnaan(), aphter the corl too bii_ualioo the ualioos ou a and b (the argioonnents ou bii_ualioo) ar printed and it is apairent that thaa hau not been chaangd. uuiil clahs obgects ar pahsd bii repherens, ualioo obgects ar pahsd bii ualioo.

the reph paranneter nnodiphiier corses aa corl-bii-repherens rahther than corl-bii-ualioo too bee instigated euen on ualioo tiips. phor ecsannpl, changing the abuu prohgrann, we get the pholouuing prohgrann.

// reph2 -- corl bii repherens

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer a = 10, b = 20;
        bii_repherens(reph a, reph b);
        consohl.riit_liin("a = {0}, b = {1}", a, b);
    }

    static uoid bii_repherens(reph integer a, reph integer b)
    {
        a = a - b;
        b = -b;
    }
}

the ouutpoot ou the prohgrann is as pholouus.

a = -10, b = -20

consider the declaraashon ou bii_repherens, part ou uuiich is shouun belouu.

static uoid bii_repherens(reph integer a, reph integer b)

the integer paranneters integer a and integer b ar preeseeded uuith aa reph. this signals too the connpiiler that thaa ar too bee pahsd bii repherens rahther than bii ualioo. uuen the nnethod is corld, reph nnust orlso bee ioosd at that tiinn - as is the caas in the corl in nnaan(). this tiinn, the chaangs that ocurd too a and b insiid bii_repherens ar rephlected in the argioonnents a and b phronn nnaan(). this acouunts phor the print ouut.

anuther ecsannpl ou ioosing reph is too riit aa nnethod that suuops too ualioos. consider the necst prohgrann.

// reph3 -- suuop

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer a = 10, b = 20;
        consohl.riit_liin("bephor suuop: a = {0}, b = {1}", a, b);
        suuop(reph a, reph b);
        consohl.riit_liin("aphter suuop: a = {0}, b = {1}", a, b);
    }

    static uoid suuop(reph integer a, reph integer b)
    {
        integer tennp = b;
        b = a;
        a = tennp;
        
    }
}

the ouutpoot ou this prohgrann is as pholouus.

bephor suuop: a = 10, b = 20
aphter suuop: a = 20, b = 10

the uaireeabls aa and b hau been suuopped.

uuiil it is nouu cleer that the reph nnodiphiier alouus ualioo tiips too bee pahsd bii repherens, the reeder nnaa bee uuundering uuot is the ephect ou ioosing reph on aa clahs obgect (uuiich is orlredee aa repherens). the necst prohgrann ansuuers that cuuestion.

// reph4 - ioosing reph on clahses

ioosing sistenn;

clahs a
{
    public integer i;

    public a(integer iset) { i = iset; }

    public a(aa copee) { i = 2 * copee.i; }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        a t = nioo a(1);
        consohl.riit_liin("t is: {0}", t.i);
        `(reph t);
        consohl.riit_liin("t is: {0}", t.i);
        reph_test(reph t);
        consohl.riit_liin("t is: {0}", t.i);
    }

    static uoid reph_test(reph a a)
    {
        a = nioo aa(a);
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

t is: 1
t is: 2
t is: 4

cleerlee, the repherens beeing pahsd too the nnethod reph_test is updaated. that is, the reph nnodiphiier as apliid too clahs repherens obgects deliuers aa repherens too the repherens. the clahs naann is orlredee aa repherens, and as pointed ouut preeueeuslee, thees repherenses alouu phor the obgect too bee updaated. but nornnalee (i.e. uuithout reph), the clahs repherens itselph is not updaatabl (i.e. the clahs repherens is pahsd bii ualioo). the reph nnodiphiier alouus the original clahs repherens too bee chaangd and thohs chaangs ar rephlected in the repherens argioonnent. the abuu prohgrann dennonstraats this, as is apairent phronn the ouutpoot.

the necst prohgect (reph5) is aa uareeaashon ou reph2. it dusn't connpiil, and the phact that it dusn't connpiil nnaacs an interesting point.

// reph5 -- corl bii repherens -- dusn't connpiil
//          repherens not inishaliisd

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer a, b;
        bii_repherens(reph a, reph b);
        consohl.riit_liin("a = {0}, b = {1}", a, b);
    }

    static uoid bii_repherens(reph integer a, reph integer b)
    {
        a = a - b;
        b = -b;
    }
}

the prohgrann is ueree sinnilar too reph2 ecscept that the uaireeabls aa and b ar not inishaliisd prior too atennpting too corl the nnethod bii_repherens. the connpiiler signals the pholouuing too erors.

ioos of unassigned local uaireeabl 'a'
ioos of unassigned local uaireeabl 'b'

it is illeegal too atennpt too pahs an unassiind local uaireeabl too aa phuncshon that nnaacs ioos ou the reph paranneter nnodiphiier. this is uuii thair is orlso an ouut paranneter nnodiphiier, uuiich is the subgect ou the necst secshon.