For instance, what is an instance?

Hi everybody,

I am having a hard time with some Java semantics. Could anyone explain to me, the difference between aclass object and theinstance of a class. An analogy will be highly appreciated.

Pardon me. I am still learning the elements of the language.

ue_Joe.

[305 byte] By [ue_Joea] at [2007-11-27 11:21:03]
# 1

Essentially equivalent semantics. See this tutorial for furtner explanation:

http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html

ChuckBinga at 2007-7-29 14:46:17 > top of Java-index,Java Essentials,New To Java...
# 2

> Essentially equivalent semantics. See this tutorial

> for furtner explanation:

> http://java.sun.com/docs/books/tutorial/java/javaOO/ob

> jectcreation.html

Perhaps not. He may be talking about the instance of java.lang.Class rather than an instance of a class.

OP, your question is tricky, it contains some terminology which has a particular meaning, but may be misconstrued as meaning something else. What Chuck posted may well be what you mean, but you may also be talking about the Class object which represents a class.

In that case, the difference is the instance is an object as defined by the class in question, whereas the class object is a special object which describes the properties of that class. So the instance can be used to do the work the class was written to do, and the class object can be used to find out information about the class, such as what methods it defines

But from the exact wording of your question, it's not clear which is correct in your context !

georgemca at 2007-7-29 14:46:17 > top of Java-index,Java Essentials,New To Java...
# 3

> Hi everybody,

> I am having a hard time with some Java semantics.

> Could anyone explain to me, the difference between a

> class object and the instance of a

> class.

http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

A class is like a template, or a blueprint. It defines what a certain type can do. For instance, the String class defines that a string can be constructed from an array of char, has a length method, a substring method, etc.

An instance is an object that's created according to one of those templates.

// definition of the class Foo

public class Foo {

private String name;

public Foo(String name) {

this.name = name;

}

public void printName() {

System.out.println(name);

}

}

...

// creating an instance of Foo

Foo foo = new Foo("fred");

First we define the class Foo. Then we create an instance of Foo that has the name "fred" and point a reference variable called "foo" at it.

Now, when you say "class object," that's a bit strange. I can't recall having seen that terminology. "Class object", on the other hand (captial "C") refers to an instance of the class java.lang.Class.

For every class you define, there is an instance of java.lang.Class that contains metadata specifying that class' name, methods, and member variables.

Class fooClass = Foo.class;

Class fooClass = Class.forName("Foo");

Both of the above give us a reference to the Class object for our Foo class.

http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

jverda at 2007-7-29 14:46:17 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks folks. The answers have shared lights on the issue. Before today I have used object of a class and instance of a class interchangeably. I came accross the topic Class object and so I got confused about the different bewteen object of a class and Class object. Actaully, here's the bit of code that prompted my question:

Class <FunnyGuy> joker = FunnyGuy.class;

FunnyGuy clown = joker.newInstance();

So, the question that came to mind was if joker is a reference to a class object and clown is a reference to a class instance, then what's the difference. Is a class instance not an object of a class anymore?

Well, now I am beginning to see the difference. But I will appreciate further illustrations.

eu_joe

ue_Joea at 2007-7-29 14:46:17 > top of Java-index,Java Essentials,New To Java...
# 5

> Thanks folks. The answers have shared lights on the

> issue. Before today I have used object of a class

> and instance of a class interchangeably.

"Instance" and "object" are fairly interchangeable.

> Class <FunnyGuy> joker = FunnyGuy.class;

Gives a reference to the Class object (instance of java.lang.Class) that has metadata about the class FunnyGuy.

> FunnyGuy clown = joker.newInstance();

Creates an instance of FunnyGuy--a FunnyGuy object. Much as if you had done new FunnyGuy().

> So, the question that came to mind was if joker is a

> reference to a class object

joker is a reference to an instance of java.lang.Class.

> and clown is a reference

> to a class instance

clown is a reference to an instance of FunnyGuy.

>, then what's the difference. Is

> a class instance not an object of a class anymore?

Um, I don't even know what that question means. :-)

> Well, now I am beginning to see the difference. But

> I will appreciate further illustrations.

Not sure what else you still don't understand.

jverda at 2007-7-29 14:46:17 > top of Java-index,Java Essentials,New To Java...
# 6

Thank you Sir! Please confirm this undertsanding of the answers so above:

1. A class has an object that holds information about it.

2.Objects can be created from a class to perform programming tasks.

ue_Joe

ue_Joea at 2007-7-29 14:46:18 > top of Java-index,Java Essentials,New To Java...
# 7

> Thank you Sir! Please confirm this undertsanding of

> the answers so above:

>

> 1. A class has an object that holds information

> about it.

If you mean the Class object, yes.

java.lang.String is a class.

java.lang.Class is a class.

Our Foo above is a class.

For each one of those, there exists an instance of java.lang.Class that holds metadata about it--which methods and fields it has, etc.

> 2.Objects can be created from a class to perform

> programming tasks.

I wouldn't say "from" a class, but yes. Given a class that defines what a String or Date or Foo is, we can create objects--Strings, Dates, Foos--that lets us do things defined by those classes.

jverda at 2007-7-29 14:46:18 > top of Java-index,Java Essentials,New To Java...
# 8

True understanding will come by doing.

petes1234a at 2007-7-29 14:46:18 > top of Java-index,Java Essentials,New To Java...
# 9

I think you probably understand this better than you think you do, and you may be looking for deeper or more formal meaning than is really there.

Read the tutorial I provided in my first post, or a textbook, and write some code. You'll develop a feel for what classes and objects are.

jverda at 2007-7-29 14:46:18 > top of Java-index,Java Essentials,New To Java...
# 10

Now I can move on! You have all been wonderful tonight. Bye.

ue_Joe

ue_Joea at 2007-7-29 14:46:18 > top of Java-index,Java Essentials,New To Java...