Results 1 to 8 of 8
Thread: pass by reference?
- 12-24-2012, 10:00 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
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)
- 12-24-2012, 10:05 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
- 12-24-2012, 10:40 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
- 12-24-2012, 01:05 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: pass by reference?
? No why? y isn`t a static variable!
- 12-24-2012, 03:14 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
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.
- 12-24-2012, 03:17 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: pass by reference?
static int x;Java Code:public class IdentifyMyParts { public static int x = 7; public int y = 3; }
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)
- 12-24-2012, 03:21 PM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-24-2012, 06:36 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
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
-
pass reference to constructions/methods
By oszc in forum New To JavaReplies: 1Last Post: 09-05-2011, 03:06 AM -
static variable /pass by reference
By katturv in forum New To JavaReplies: 15Last Post: 10-03-2010, 08:17 AM -
How to create pass by reference??
By --> xeiyne! in forum CLDC and MIDPReplies: 4Last Post: 04-08-2010, 06:43 PM -
Don't pass by reference
By Lyven in forum Advanced JavaReplies: 6Last Post: 11-16-2009, 06:06 PM -
Pass-by-reference
By popjava in forum New To JavaReplies: 1Last Post: 10-19-2009, 03:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks