final class, final <variable> or <data member>
whats the purpose of declaring a class as final.??
Code :
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?
Re: final class, final <variable> or <data member>
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.
Re: final class, final <variable> or <data member>
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:
Code :
public class MyUtilityClass {
protected MyUtilityClass() {
throw new UnsupportedOperationException();
}
// public static methods go here...
}
// Json
Re: final class, final <variable> or <data member>
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?
Re: final class, final <variable> or <data member>
but the Date class has public constructor...its a utility class..
Re: final class, final <variable> or <data member>
No, take this example:
Code :
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).
Re: final class, final <variable> or <data member>
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
Re: final class, final <variable> or <data member>
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?
Re: final class, final <variable> or <data member>
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
Re: final class, final <variable> or <data member>
kinda confusing.. anyway ill be able to understand all those things incrementaly , but tnx for that!!
\m/