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.

Results 1 to 10 of 10

Thread: final class, final <variable> or <data member>

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default final class, final <variable> or <data member>

    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?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 17th, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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:

    public class MyUtilityClass {
     
        protected MyUtilityClass() {
            throw new UnsupportedOperationException();
        }
     
        // public static methods go here...
    }

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (September 17th, 2009)

  6. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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?

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: final class, final <variable> or <data member>

    but the Date class has public constructor...its a utility class..

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: final class, final <variable> or <data member>

    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).

  9. The Following User Says Thank You to helloworld922 For This Useful Post:

    Json (September 18th, 2009)

  10. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  11. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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?

  12. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  13. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: final class, final <variable> or <data member>

    kinda confusing.. anyway ill be able to understand all those things incrementaly , but tnx for that!!


Similar Threads

  1. New member
    By 5723 in forum Member Introductions
    Replies: 5
    Last Post: May 11th, 2012, 01:57 PM
  2. Java program to display single and plural words
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 11th, 2009, 12:09 AM
  3. New member
    By henry in forum Member Introductions
    Replies: 2
    Last Post: May 13th, 2009, 02:31 PM
  4. New member
    By cowgirl1976 in forum Member Introductions
    Replies: 1
    Last Post: April 20th, 2009, 04:30 AM
  5. [SOLVED] Some important questions about java programming
    By chronoz13 in forum Java Theory & Questions
    Replies: 12
    Last Post: April 16th, 2009, 03:55 PM