Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Is there any way to require a class to declare a variable?

Thread view: 
Inertia_sublimation - 06 May 2004 01:59 GMT
Hello everyone!
This may be a bit of a dumb question, but I think this is the only way to
get where I want to be.

Lets say I have an abstract BottledWater class and two classes, Evion and
Aquafina extending from it.

public abstract class BottledWater {
   abstract public static final String NAME; // Compile-time error.
}

public class Aquafina extends BottledWater {
   public static final String NAME = "Aquafina";
}

public class Evion extends BottledWater {
   public static final String NAME = "Evion";
}

Currently, the abstract constant declaration in BottledWater gives a
"Modifyer abstract not allowed here" compile-time error. Is there any way to
require Evion and Aquafina declare their own NAME variable? I thought a
public static constant would be best, since in the scenerio I'm working
with, the names would always be the same, and all classes of the same type
would have the same name.

Thanks in advance!
Roedy Green - 06 May 2004 02:25 GMT
>   abstract public static final String NAME; // Compile-time error.

take out the abstract. There is no way in Java to force someone to
shadow that.

You could do it this way:

   abstract public String getName ()
then they have to provide an implementation.

This is what you want anyway.  Otherwise the only way to get the
string is via the specific class name.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Inertia_sublimation - 07 May 2004 06:13 GMT
>     abstract public String getName ()
> then they have to provide an implementation.

There is no way to require abstract static methods either? :-(
Roedy Green - 07 May 2004 06:20 GMT
>There is no way to require abstract static methods either? :-(

Nope.  Interfaces and abstract force you to create instance methods
only.

Since you can't use static stuff in a generic way, there is not much
point in putting it in an interface, though I too have often wished I
could do something like "You should write a static method like this"
to have a complete useful class.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.