Creating References to Objects
In Java, the new
keyword returns a reference to a newly-constructed object. Therefore, in
String s = new String("foo");
the value of variable s
is a reference to a String object. And then, if you were to do something like
String t = s;
then you've set t
to the same value as s
, which is to say, it's now a reference to the same object.
Related videos on Youtube
Sherifftwinkie
Updated on December 21, 2020Comments
-
Sherifftwinkie over 3 years
I have this Object here:
public class ItemInfo { String productName; String rfidTagNumber; String originalLocal; String currentLocal; double productPrice; public ItemInfo(String name, String tag, String origLoc, String curLoc, double price) { productName = name; rfidTagNumber = tag; originalLocal = origLoc; currentLocal = curLoc; productPrice = price; }
I need to create and have a reference to an
ItemInfo
Object in here:public class ItemInfoNode { ItemInfoNode next; ItemInfoNode prev; public ItemInfoNode(){ } public void setInfo(ItemInfo info) { } public ItemInfo getInfo(){ return null; } public void setNext(ItemInfoNode node) { } public void setPrev(ItemInfoNode node) { } public ItemInfoNode getNext() { return null; } public ItemInfoNode getPrev() { return null; } }
I know how to make new objects but I am not quite sure that is correct so I omitted the line, how do I create just a reference?
EDIT: Just another small sidebar, I have two ItemInfoNode objects next and prev as seen above, do I just leave the constructor blank? Is that okay? I pasted the rest of the class to show functions, if that'll help an answer in anyway. Thanks again!
-
Javier about 11 yearsNote that this is only an example. In most cases you should avoid using new String.