IntelliJ Java IDE
The moose likes Java in General and the fly likes Using a Class as a Data Type Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Using a Class as a Data Type" Watch "Using a Class as a Data Type" New topic
Author

Using a Class as a Data Type

Scott Pucket
Greenhorn

Joined: Nov 16, 2002
Posts: 3
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

Joined: May 14, 2002
Posts: 224


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
"" 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
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
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


Java API Documentation
The Java Tutorial
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
 
subject: Using a Class as a Data Type
 
Threads others viewed
Code Help
Loop Problems
need help with JPanel, liseners, and referencing vectors
please help me
regarding constructor
MyEclipse, The Clear Choice