Results 1 to 8 of 8
  1. #1
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default pass by reference?

    Hi all,

    I am new on Java, I don't why the final answer is from the below.


    Question: What is the output from the following code:

    IdentifyMyParts a = new IdentifyMyParts();
    IdentifyMyParts b = new IdentifyMyParts();
    a.y = 5;
    b.y = 6;
    a.x = 1;
    b.x = 2;
    System.out.println("a.y = " + a.y);
    System.out.println("b.y = " + b.y);
    System.out.println("a.x = " + a.x);
    System.out.println("b.x = " + b.x);
    System.out.println("IdentifyMyParts.x = " +
    IdentifyMyParts.x);
    Answer: Here is the output:

    a.y = 5
    b.y = 6
    a.x = 2
    b.x = 2
    IdentifyMyParts.x = 2



    Please advise.
    source:
    Answers to Questions and Exercises: Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: pass by reference?

    The explanation stands still underneath?
    "Because x is defined as a public static int in the class IdentifyMyParts, every reference to x will have the value that was last assigned because x is a static variable"

    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) - static keyword

  3. #3
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: pass by reference?

    Quote Originally Posted by eRaaaa View Post
    The explanation stands still underneath?
    "Because x is defined as a public static int in the class IdentifyMyParts, every reference to x will have the value that was last assigned because x is a static variable"

    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) - static keyword
    then i have confuse that y should also be a static variable, right?
    if yes, then why the overall answer is not like below,
    a.y = 6
    b.y = 6
    a.x = 2
    b.x = 2

    Please advise.
    Thanks.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: pass by reference?

    ? No why? y isn`t a static variable!

  5. #5
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: pass by reference?

    Thanks for reply.
    Pls accept my apologize that i am just new on java.
    Thus i can not realize clearly on how to know that y is non-static.
    would you mind telling me instead?

    Thanks a lot.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: pass by reference?

    Java Code:
    public class IdentifyMyParts {
        public static int x = 7;
        public int y = 3;
    }
    static int x;
    and only int y; there is no static ?
    did you read my link? -> Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: pass by reference?

    About the subject line: there is no pass-by-reference in Java. Primitives are passed by value. References are passed by value. Objects are not passed at all.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: pass by reference?

    I am really sorry about that I have overlooked the

    public class IdentifyMyParts {
    public static int x = 7;
    public int y = 3;
    }

    which stated at the beginning...i have just focused on the Q1 part C at all....
    anyway, i am now realize why the answer is...

    Thanks a lot eRaaa for your patient.

Similar Threads

  1. pass reference to constructions/methods
    By oszc in forum New To Java
    Replies: 1
    Last Post: 09-05-2011, 03:06 AM
  2. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 08:17 AM
  3. How to create pass by reference??
    By --> xeiyne! in forum CLDC and MIDP
    Replies: 4
    Last Post: 04-08-2010, 06:43 PM
  4. Don't pass by reference
    By Lyven in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 06:06 PM
  5. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 03:45 PM

Posting Permissions

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