I'm having trouble using a class as a data type. i have 3 classes called Item, Vehicle(extends Item) and Registration. I need to use some methods in the Registration class in my Vehicle class. My teacher recommended using the class Registration as the data type(ie. Registration permit = new Registration() but when i try to initialize it in my default constructor as permit = ""; it gives me and incompatible types found. my question is... what would i initialize them as to make them work? thanks Scott
If the methods of class Registration are declared static you are not even required to instantiate the class since you may use the class method(s) directly using the class name. Did it help? Do get back [ February 14, 2003: Message edited by: Debashish Chakrabarty ]
Debashish
SCJP2, SCWCD 1.4
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
"" is a String object, while permit can only hold a Registration object or subclasses thereof. That's why you cant assign "" to permit. Try permit = null. Null is a special value to indicate that permit doesn't contain any Registration object at all. You can use null for any object type. - Peter [ February 12, 2003: Message edited by: Peter den Haan ]
Scott Pucket
Greenhorn
Joined: Nov 16, 2002
Posts: 3
posted
0
I'll have to try both of those solutions. I also found a solution after HOURS of dinking with it. I found: Registration permit; then in the default constructor you add: permit = new Registration(); instead of permit = ""; Thanks for helping me out though
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Normally, in the constructor you will initialize object references as you have said: allocating an object with new. However, if you want to delay allocation, you could set the reference to null. Just be careful that you don't try to call methods until you actually allocate an object, as it will through an exception. HTH Layne
If permit is a pointer to objects of type Registration then you can't assign an empty String to permit. Registration permit; permit = ""; // generates compile error permit = null; // allowed