Results 1 to 9 of 9
  1. #1
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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.

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

    Default 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 :))

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default 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.

    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
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Java Referencing vs creating instance

    Thanks guys.

    Quote Originally Posted by Tolls View Post
    Nope, bk is a reference, the value of which is the location of the object created by 'new Bike()'.
    I thought bk is an instance of Bike. If bk is a reference of bike, does that mean I can use them interchangeably?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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:
    Java Code:
    int x = 0;
    The variable 'x' is an int primitive.
    Please do not ask for code as refusal often offends.

  7. #7
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Java Referencing vs creating instance

    I understand. Then what's an instance?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Referencing vs creating instance

    Quote Originally Posted by garnaout View Post
    I understand. Then what's an instance?
    Anything you create with the 'new' operator is an instance of some class; in your example that Bike is an instance of your Bike class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Replies: 14
    Last Post: 07-21-2011, 08:56 PM
  2. Finding and Creating Instance of Every Class in Package
    By Julian Trust in forum New To Java
    Replies: 5
    Last Post: 06-25-2011, 08:34 AM
  3. Help with creating an instance of a class extension
    By Inferno719 in forum New To Java
    Replies: 19
    Last Post: 05-04-2011, 03:53 AM
  4. Replies: 10
    Last Post: 01-06-2010, 08:33 AM
  5. "endless" eventLoop when creating new Instance
    By javacafe in forum Eclipse
    Replies: 0
    Last Post: 08-10-2009, 06:37 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
  •