• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a Class as a Data Type  RSS feed

 
Scott Pucket
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Debashish Chakrabarty
Ranch Hand
Posts: 234
Java Python Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"" 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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!