whats the purpose of declaring a class as final.??
public final class <ClassName>
and ive notice the Math class cannot be instantiated. why? is that because of the 'final' declaration or
is it becage of the default constructos that is 'private?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
whats the purpose of declaring a class as final.??
public final class <ClassName>
and ive notice the Math class cannot be instantiated. why? is that because of the 'final' declaration or
is it becage of the default constructos that is 'private?
You declare classes final if you don't want anyone to be able to extend your class. This is the case with the String class, which is declared final.
Final classes can be instantiated. There may be several reasons why the Math class can't be instantiated: constructors are private (as you said), or the class is declared abstract.
chronoz13 (September 17th, 2009)
Usually when you create a utility class such as Math you want to mark the constructors as private or protected because it would not make any sense instantiating a class that has only static methods.
My normal way of writing default constructors for my utility classes are:
public class MyUtilityClass { protected MyUtilityClass() { throw new UnsupportedOperationException(); } // public static methods go here... }
// Json
chronoz13 (September 17th, 2009)
so there are certain situations that when your class has static methods you should declare
your constrcutor as private.?
or IT MUST be private everytime your are creating a class with static methods?
but the Date class has public constructor...its a utility class..
No, take this example:
public final SomeClass { public static double doIt() { return 5.0; } public static double doIt2() { return 3.0; } }
In theory, if you tried creating a SomeClass object, it would compile and run perfectly fine. However, there are no public instance methods/fields (in this case, none at all) that you would be able to use. To make it "safer", it's best to add the code Json had, this way SomeClass could never be instantiated.
Another way to prevent a class from being instantiated is declare it abstract, though this is NOT recommended for classes only used statically, and I think illegal on classes declared final (don't take my word on the second statement, though).
Json (September 18th, 2009)
Yes you cannot have a final abstract class. There is nothing that says you MUST do what I recommended but its best practice and for your own good really
But if you wish you can do just what helloworld showed you there.
// Json
another question.
about Gregorian Calendar class. in general convention they say that it is inefficient if you use an 'INSTANCE' or 'OBJECT' to call a static method (which belongs to a class), i red a book and i saw an exercise about a calendar ,
and i saw that it can instantiate an object.? is that fine? or maybe its just an exercise for beginners?
GregorianCalendar yes, Calendar no.
Does that make sense? There are 3 classes that you should use a factory method for and that is Calendar, DateFormat and NumberFormat. So say the SCJP studying I've been doing lately.
// Json
kinda confusing.. anyway ill be able to understand all those things incrementaly , but tnx for that!!