Yes, that is correct. The syntax "new DoThis()" is used to create a reference to the object 'DoThis'.
The syntax "DoThis dt" means you are declaring 'dt' do be of type "DoThis".
Gabriel Allen
Greenhorn
Joined: Jan 09, 2007
Posts: 12
posted
0
Thanks. But, does the "DoThis" in the "DoThis dt" have to be the name of the object being created: "new DoThis"?
So, I can't say:"ThisThing dt = new DoThis()"; right? [ January 12, 2007: Message edited by: Gabriel Allen ]
Abdulla Mamuwala
Ranch Hand
Joined: Jan 09, 2004
Posts: 225
posted
0
You can say 'ThisThing dt = new DoThis()' provided 'DoThis()' is a subclass of 'ThisThing'. That is you can say 'Car ct = new Honda()' or 'Car ct = new Ford()' because Honda and Ford are types of cars or are a sub class of cars. 'Crazy cz = new President()' you figure it out
Gabriel Allen
Greenhorn
Joined: Jan 09, 2007
Posts: 12
posted
0
Thanks! So, there has to be some kind of association, either class name or subclass name, that you have to declare as the object type?
Ah! Now is see! If the object is "DoThis", the object type has to be "DoThis"? So, we are infact saying,"int newNumber = new int()". Is that right?
Originally posted by Gabriel Allen: Thanks! So, there has to be some kind of association, either class name or subclass name, that you have to declare as the object type? ...
Yes.
Variable name
A variable name can be any legal identifier. But whenever you declare a variable, you also need to specify its type, which is a class or interface name.
Type variableName;
Object creation
When you create a new instance, you use the keyword "new" followed by a constructor call. Constructor names match the class name exactly, and this denotes the true type of the object.
new Type();
Assignment
When you assign an object reference to a variable, the object type must be consistent with the variable type. With inheritance, a subtype IS-A supertype. (For example, if Cat extends Animal, then a Cat IS-AN Animal.) So, as Abdulla pointed out, you can have...
SuperType variableName = new SubType();
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Originally posted by Gabriel Allen: ..."int newNumber = new int()". Is that right?
The idea is right, but int is a primitive type -- not an object. So instead of creating a new object by calling a constructor, you simply assign a value.
int i = 7;
However, there is a wrapper type that represents an int value as an object. This is called Integer...
Integer in = new Integer(7);
(And then there's a feature called "boxing," but we won't confuse matters with that here.) [ January 12, 2007: Message edited by: marc weber ]
Gabriel Allen
Greenhorn
Joined: Jan 09, 2007
Posts: 12
posted
0
Thanks! Now I got! Or so I think.
Sorry for the "int" example. I was just trying use it as an example in the sense it is a specific type and you have to assign variables a type. Poor analogy, but I think I got the idea across. Ha!