Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What's the difference between and instance and an object?

Reply
Thread Tools

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

 
 
Chad
Guest
Posts: n/a
 
      06-06-2011
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
 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      06-06-2011
..
 
Reply With Quote
 
 
 
 
Patrick
Guest
Posts: n/a
 
      06-06-2011
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().
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-06-2011
On Jun 6, 9:18*am, Patrick <(E-Mail Removed)> 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
 
Reply With Quote
 
Mayeul
Guest
Posts: n/a
 
      06-06-2011
On 06/06/2011 18:33, Chad wrote:
> On Jun 6, 9:18 am, Patrick<(E-Mail Removed)> 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
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-06-2011
On Jun 6, 9:54*am, Mayeul <(E-Mail Removed)> wrote:
> On 06/06/2011 18:33, Chad wrote:
>
> > On Jun 6, 9:18 am, Patrick<(E-Mail Removed)> *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
 
Reply With Quote
 
Abu Yahya
Guest
Posts: n/a
 
      06-08-2011
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?
 
Reply With Quote
 
Abu Yahya
Guest
Posts: n/a
 
      06-08-2011
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'.
 
Reply With Quote
 
Abu Yahya
Guest
Posts: n/a
 
      06-08-2011
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.
 
Reply With Quote
 
Lewis Bloch
Guest
Posts: n/a
 
      06-10-2011
On Jun 6, 9:33*am, Chad <(E-Mail Removed)> wrote:
> On Jun 6, 9:18*am, Patrick <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between Class variables and Instance variables? rahul8143@gmail.com Java 10 06-06-2011 06:43 AM
How do I tell the difference between a class C and the singletonclass of an instance of C deanwampler@gmail.com Ruby 3 11-02-2007 08:52 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
difference between class methods and instance methods John M. Gabriele Python 18 02-18-2005 05:17 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments