Here is a java program I dont understand how does the proces

Here is a java program, I dont understand how does the process work, can you explain to me? thank you

class Letter {

   char c;

}

public class PassObject_2 {

static void f(Letter y) {

       y.c = \'z\';

   }

public static void main(String[] args) {

       Letter x = new Letter();

       x.c = \'a\';

       System.out.println(\"1: x.c: \" + x.c);

       f(x);

       System.out.println(\"2: x.c: \" + x.c);

   }

}

output

1: x.c: a

2: x.c: z

Solution

// creating class Letter
public class Letter {

   // declare character c
    char c;

}

//create other class PassObject_2
public class PassObject_2
{
   // create function and passing class name as parameter
   // the method f would appear to be making a copy of its argument Letter y inside the scope of method
   static void f(Letter y)
   {

       // function is called by using object of class Letter
        y.c = \'z\';

    }

    public static void main(String[] args) {
//creating and initializing object of class
        Letter x = new Letter();
// calling the Letter class
        x.c = \'a\';
// display value of x.c that is a
        System.out.println(\"1: x.c: \" + x.c);

       // function call
       // calling static void f method and passing argument as x and it stores value as z
        f(x);

       // display value of f(x)
        System.out.println(\"2: x.c: \" + x.c);

    }

}

output

1: x.c: a

2: x.c: z

Here is a java program, I dont understand how does the process work, can you explain to me? thank you class Letter { char c; } public class PassObject_2 { stati
Here is a java program, I dont understand how does the process work, can you explain to me? thank you class Letter { char c; } public class PassObject_2 { stati

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site