classes and objects

i'm a java newbie and i need to get something basic cleared up.Refering to the line below, is it correct that Label is a class and theLabel is an object? And i could change the name of the object if i wanted?Label theLabel = new Label("hello");ThanksMark
[290 byte] By [mark7908a] at [2007-10-3 1:38:49]
# 1
Yes and yes.
ValentineSmitha at 2007-7-14 18:37:00 > top of Java-index,Java Essentials,New To Java...
# 2

> And i could

> change the name of the object if i wanted?

Normally one doesn't change the names of objects after they are created. There isn't usually any point. However,

Label theLable2 = new Label();

theLabel2 = theLabel;

will make theLabel2 control the same object that theLabel controls.

Asbestosa at 2007-7-14 18:37:00 > top of Java-index,Java Essentials,New To Java...
# 3

> Refering to the line below, is it correct that Label

> is a class and theLabel is an object?

Label is a class, yes. No, theLabel is NOT an object. It is a variable, which can contain a reference to an object of type Label. Do not confuse variables with objects.

> And i could change the name of the object if i wanted?

Objects do not have names. You can't change the name of a variable after declaring it. You could certainly write a program that is identical to what you have there, except that you named the variable "theDonald" instead of "theLabel".

DrClapa at 2007-7-14 18:37:00 > top of Java-index,Java Essentials,New To Java...
# 4
ok i'm a little confused now, label is a class but what is theLabel? Some tell me an object, others a variable.Label theLabel = new Label("hello");Can anyone else help?Mark.
mark1908a at 2007-7-14 18:37:00 > top of Java-index,Java Essentials,New To Java...
# 5

> ok i'm a little confused now, label is a class but

> what is theLabel? Some tell me an object, others a

> variable.

Depends. Variables are declared inside methods. Otherwise, it might as well be an attribute. But it is not an object.

What it is: it's a reference named theLabel, of type JLabel. Regardless of what it's pointing at.

> Label theLabel = new Label("hello");

In this case, the reference "theLabel" is pointing at an instance of Label.

Just to point out the difference between the reference type and the actual type of the instance:

Object o = new JLabel(); // reference is of the instance's supertype

List a = new ArrayList(); // reference is of the type of the interface implemented by the instance

Jlabel label = null; // reference of type JLabel points to the null type

On this note: casting can change the type of the reference, narrowing or broadening a "view" onto the instance moving along the inheritance hierarchy. It does not change the type of the instance behind it!

CeciNEstPasUnProgrammeura at 2007-7-14 18:37:00 > top of Java-index,Java Essentials,New To Java...