What's the difference between and instance and an object?

Discussion in 'Java' started by Chad, Jun 6, 2011.

  1. Chad

    Chad Guest

    Let's say I have the following...

    public class Main {

    public static void main(String[] args) {
    MyStack stack = new MyStack();

    System.out.println("Instance of Object: "
    + (stack instanceof Object));

    System.out.println("Instance of MyStack: "
    + (stack instanceof MyStack));


    }
    }//end Main

    class MyStack {

    private int size = 0;

    public int tripleIt(int newSize) {
    size = newSize;
    return 3 * size;
    }
    }

    I get the following output when I run this code...

    Instance of Object: true
    Instance of MyStack: true


    How can there be an instance of MyStack when I never made a
    constructor for it? That is, I never create a MyStack object.


    Chad
     
    Chad, Jun 6, 2011
    #1
    1. Advertising

  2. Chad

    Chad Guest

    Re: What's the difference between an instance and an object?

    ..
     
    Chad, Jun 6, 2011
    #2
    1. Advertising

  3. Chad

    Patrick Guest

    Le 06/06/2011 18:08, Chad a écrit :
    >
    > How can there be an instance of MyStack when I never made a
    > constructor for it? That is, I never create a MyStack object.


    You always have a constructor, even if implicit.
    You created an instance with new MyStack().
     
    Patrick, Jun 6, 2011
    #3
  4. Chad

    Chad Guest

    On Jun 6, 9:18 am, Patrick <> wrote:
    > Le 06/06/2011 18:08, Chad a écrit :
    >
    >
    >
    > > How can there be an instance of MyStack when I never made a
    > > constructor for it? That is, I never create a MyStack object.

    >
    > You always have a constructor, even if implicit.
    > You created an instance with new MyStack().


    Maybe I'm acting like a dweeb about this, but according to the Java
    Docs, when there is no explicit constructor, then the implict
    constructor is Object. I think that is how they say it. So I figured
    that when I created instance of MyStack with 'new MyStack()', that the
    only instance would be Object since I omitted the MyStack constructor.

    Chad
     
    Chad, Jun 6, 2011
    #4
  5. Chad

    Mayeul Guest

    On 06/06/2011 18:33, Chad wrote:
    > On Jun 6, 9:18 am, Patrick<> wrote:
    >> Le 06/06/2011 18:08, Chad a écrit :
    >>
    >>
    >>
    >>> How can there be an instance of MyStack when I never made a
    >>> constructor for it? That is, I never create a MyStack object.

    >>
    >> You always have a constructor, even if implicit.
    >> You created an instance with new MyStack().

    >
    > Maybe I'm acting like a dweeb about this, but according to the Java
    > Docs, when there is no explicit constructor, then the implict
    > constructor is Object. I think that is how they say it.


    This is not a matter of opinion.

    Here is what the Java Docs have to say:
    http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9

    > So I figured
    > that when I created instance of MyStack with 'new MyStack()', that the
    > only instance would be Object since I omitted the MyStack constructor.


    As you could verify with your simple test, that is incorrect.
    For starters, for any symbol 'A' denoting a class, 'new A()' will always
    either:
    - build an instance of the class 'A'
    - throw an Exception
    - refuse to compile

    Same goes if this call is given parameters.

    In other words, it cannot possibly build an instance of any other class,
    neither Object nor anything.

    --
    Mayeul
     
    Mayeul, Jun 6, 2011
    #5
  6. Chad

    Chad Guest

    On Jun 6, 9:54 am, Mayeul <> wrote:
    > On 06/06/2011 18:33, Chad wrote:
    >
    > > On Jun 6, 9:18 am, Patrick<>  wrote:
    > >> Le 06/06/2011 18:08, Chad a crit :

    >
    > >>> How can there be an instance of MyStack when I never made a
    > >>> constructor for it? That is, I never create a MyStack object.

    >
    > >> You always have a constructor, even if implicit.
    > >> You created an instance with new MyStack().

    >
    > > Maybe I'm acting like a dweeb about this, but according to the Java
    > > Docs, when there is no explicit constructor, then the implict
    > > constructor is Object. I think that is how they say it.

    >
    > This is not a matter of opinion.
    >
    > Here is what the Java Docs have to say:http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9
    >
    > > So I figured
    > > that when I created instance of MyStack with 'new MyStack()', that the
    > > only instance would be Object since I omitted the MyStack constructor.

    >
    > As you could verify with your simple test, that is incorrect.
    > For starters, for any symbol 'A' denoting a class, 'new A()' will always
    > either:
    > - build an instance of the class 'A'
    > - throw an Exception
    > - refuse to compile
    >
    > Same goes if this call is given parameters.
    >
    > In other words, it cannot possibly build an instance of any other class,
    > neither Object nor anything.
    >


    Sons of witches. I didn't pay enough attention to the docs when I read
    them the first time around. It happens.

    Chad
     
    Chad, Jun 6, 2011
    #6
  7. Chad

    Abu Yahya Guest

    On 6/6/2011 10:24 PM, Mayeul wrote:
    > In other words, it cannot possibly build an instance of any other class,
    > neither Object nor anything.


    Won't 'A' be an instanceOf Object?
     
    Abu Yahya, Jun 8, 2011
    #7
  8. Chad

    Abu Yahya Guest

    On 6/8/2011 9:21 PM, Abu Yahya wrote:
    > On 6/6/2011 10:24 PM, Mayeul wrote:
    >> In other words, it cannot possibly build an instance of any other class,
    >> neither Object nor anything.

    >
    > Won't 'A' be an instanceOf Object?


    Err...I mean instanceof, with a lower-case 'o'.
     
    Abu Yahya, Jun 8, 2011
    #8
  9. Chad

    Abu Yahya Guest

    On 6/8/2011 10:03 PM, Patricia Shanahan wrote:

    > There are two slightly different meanings of "X is an instance of Y".
    >
    > 1. X is an object whose class is Y. That is the meaning in which a
    > constructor can only return an instance of its own class. The expression
    > (new MyClass()), if it completes without exception or error, is always
    > an object whose class is MyClass.
    >
    > 2. X is an object whose class implements or extends Y. This is the
    > meaning that is tested by the "instanceof" operator. The expression
    >
    > (x instanceof Y)
    >
    > is true if x references an object whose class implements or extends Y.
    >
    > (x instanceof Object) is true unless x is null.



    Thanks. That makes it /much/ clearer now.
     
    Abu Yahya, Jun 8, 2011
    #9
  10. Chad

    Lewis Bloch Guest

    On Jun 6, 9:33 am, Chad <> wrote:
    > On Jun 6, 9:18 am, Patrick <> wrote:
    >
    > > Le 06/06/2011 18:08, Chad a écrit :

    >
    > > > How can there be an instance of MyStack when I never made a
    > > > constructor for it? That is, I never create a MyStack object.

    >
    > > You always have a constructor, even if implicit.
    > > You created an instance with new MyStack().

    >
    > Maybe I'm acting like a dweeb about this, but according to the Java
    > Docs, when there is no explicit constructor, then the implict
    > constructor is Object. I think that is how they say it. So I figured


    That is not correct. I know of no place in any of the standard API's
    Javadocs that make any statement that could be understood that way. I
    don't even know where in the Javadocs you would hope to find such a
    statement.

    > that when I created instance of MyStack with 'new MyStack()', that the
    > only instance would be Object since I omitted the MyStack constructor.


    No.

    When you omit an explicit constructor, one *for that very type* is
    provided by the compiler, so the implicit constructor in your case is
    'public MyStack()'.

    "All classes have at least one constructor. If a class does not
    explicitly declare any, the Java compiler automatically provides a no-
    argument constructor, called the default constructor."
    <http://download.oracle.com/javase/tutorial/java/javaOO/
    objectcreation.html>

    --
    Lew
     
    Lewis Bloch, Jun 10, 2011
    #10
  11. Chad

    Lewis Bloch Guest

    On Jun 8, 8:51 am, Abu Yahya <> wrote:
    > On 6/6/2011 10:24 PM, Mayeul wrote:
    >
    > > In other words, it cannot possibly build an instance of any other class,
    > > neither Object nor anything.

    >
    > Won't 'A' be an instanceOf Object?


    An object of any type T is automatically and inherently an instance of
    any type S that is a supertype of T, by the very definition of
    subtyping. Subtypes embody the /is-a/ relationship.

    --
    Lew
     
    Lewis Bloch, Jun 10, 2011
    #11
  12. Chad

    Abu Yahya Guest

    On 6/10/2011 11:36 PM, Lewis Bloch wrote:
    > On Jun 8, 8:51 am, Abu Yahya<> wrote:
    >> On 6/6/2011 10:24 PM, Mayeul wrote:
    >>
    >>> In other words, it cannot possibly build an instance of any other class,
    >>> neither Object nor anything.

    >>
    >> Won't 'A' be an instanceOf Object?

    >
    > An object of any type T is automatically and inherently an instance of
    > any type S that is a supertype of T, by the very definition of
    > subtyping. Subtypes embody the /is-a/ relationship.
    >


    Thanks, Lew, for the explanation.
     
    Abu Yahya, Jun 11, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page