Results 1 to 9 of 9
- 03-16-2012, 02:33 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Java Referencing vs creating instance
I am relatively new to Java, I want to understand the difference b/w referencing and instantiating.
I know if I have a class Bike and at some point use Bike bk = new Bike(); that would be creating an instance. But how about creating a reference? what does that mean?
thanks.
- 03-16-2012, 07:10 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Java Referencing vs creating instance
In your example bk is your reference. It references an object (Bike) (thats not 100% correct - bk is more a variable that saves the reference, but for understanding its ok i think :))
- 03-16-2012, 10:02 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Java Referencing vs creating instance
Nope, bk is a reference, the value of which is the location of the object created by 'new Bike()'.
Please do not ask for code as refusal often offends.
- 03-16-2012, 10:04 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Referencing vs creating instance
If I point (reference) an already existing bike I'm not creating a bike; if I create a bike and then point at it I'm effectively protecting the bike against the evil garbage collector; e.g.
kind regards,Java Code:Bike a= new Bike(); // create a bike and point at it Bike b= a; // point to an existing bike a= null; // b still points at the bike b= null; // now the bike can be collected
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-16-2012, 02:46 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
- 03-16-2012, 02:55 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Java Referencing vs creating instance
The non-primitive variables we use in code are references to objects on the heap.
So 'bk' is a reference to the Bike object created by 'new Bike()'.
In the same way that:
The variable 'x' is an int primitive.Java Code:int x = 0;
Please do not ask for code as refusal often offends.
- 03-16-2012, 03:00 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Java Referencing vs creating instance
I understand. Then what's an instance?
- 03-16-2012, 03:09 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Referencing vs creating instance
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-16-2012, 03:12 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Quick question about creating and instance and passing it to a constuctor in a GUI
By jokkycaz in forum New To JavaReplies: 14Last Post: 07-21-2011, 08:56 PM -
Finding and Creating Instance of Every Class in Package
By Julian Trust in forum New To JavaReplies: 5Last Post: 06-25-2011, 08:34 AM -
Help with creating an instance of a class extension
By Inferno719 in forum New To JavaReplies: 19Last Post: 05-04-2011, 03:53 AM -
get java.lang.NullPointerException while creating an instance
By Basit56 in forum New To JavaReplies: 10Last Post: 01-06-2010, 08:33 AM -
"endless" eventLoop when creating new Instance
By javacafe in forum EclipseReplies: 0Last Post: 08-10-2009, 06:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks