Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   What's the difference between and instance and an object? (http://www.velocityreviews.com/forums/t749512-whats-the-difference-between-and-instance-and-an-object.html)

Chad 06-06-2011 04:08 PM

What's the difference between and instance and an object?
 
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 06-06-2011 04:16 PM

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

Patrick 06-06-2011 04:18 PM

Re: What's the difference between and instance and an object?
 
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().

Chad 06-06-2011 04:33 PM

Re: What's the difference between and instance and an object?
 
On Jun 6, 9:18*am, Patrick <patr...@antispam.invalid> 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

Mayeul 06-06-2011 04:54 PM

Re: What's the difference between and instance and an object?
 
On 06/06/2011 18:33, Chad wrote:
> On Jun 6, 9:18 am, Patrick<patr...@antispam.invalid> 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/t...ses.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

Chad 06-06-2011 05:11 PM

Re: What's the difference between and instance and an object?
 
On Jun 6, 9:54*am, Mayeul <mayeul.marg...@free.fr> wrote:
> On 06/06/2011 18:33, Chad wrote:
>
> > On Jun 6, 9:18 am, Patrick<patr...@antispam.invalid> *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/t...ses.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

Abu Yahya 06-08-2011 03:51 PM

Re: What's the difference between and instance and an object?
 
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 06-08-2011 03:54 PM

Re: What's the difference between and instance and an object?
 
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 06-08-2011 04:37 PM

Re: What's the difference between and instance and an object?
 
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.

Lewis Bloch 06-10-2011 06:05 PM

Re: What's the difference between and instance and an object?
 
On Jun 6, 9:33*am, Chad <cdal...@gmail.com> wrote:
> On Jun 6, 9:18*am, Patrick <patr...@antispam.invalid> 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


All times are GMT. The time now is 11:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57