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

C

Chad

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
 
P

Patrick

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().
 
C

Chad

Le 06/06/2011 18:08, Chad a écrit :




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
 
M

Mayeul

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

Chad

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


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
 
A

Abu Yahya

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

Lewis Bloch

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>
 
L

Lewis Bloch

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

Abu Yahya

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top