Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:47:11+00:00 2026-05-24T17:47:11+00:00

I have a super class with telescoping constructors with a finalize() method. To protect

  • 0

I have a super class with telescoping constructors with a finalize() method. To protect against subclasses forgetting to invoke super.finalize, I have written a finalizer guardian (EJ Item 7 ) like so.

public class Super {

    public Super() {}

    {
        Object finalizerGuardian = new Object() {
            @Override
            protected void finalize() {
                System.out.println("Finalizer guardian finalize");
                Super.this.finalize();
            }
        };
    }

    protected void finalize() {
        System.out.println("Super finalize");
    }

}

Here is a sample subclass —

public class Sub extends Super {

    public Sub() {}

    protected void finalize() {
        System.out.println("Sub finalize");
    }

    public static void main(String[] args)
            throws InterruptedException {
        if (1 == 1) {
            Sub s1 = new Sub();
        }
        Runtime.getRuntime().gc();
        Thread.sleep(10000);
    }
}

When the s1 object goes out of scope, the finalizer guardian’s finalize() gets called, and I get the SYSO from the subclass’s finalize method, but never get the one from super’s finalize.

I’m confused. Am I misunderstanding something fundamentally?

Disclaimer : I realize finalizers are dangerous and not advisable, etc. Still trying to understand the problem here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:47:13+00:00Added an answer on May 24, 2026 at 5:47 pm

    Effective Java’s finalizer guardian should perform the necessary finalization logic itself (for example, call some method of Super that performs the actual finalization) rather than call the finalize() method, because in your case Super.this.finalize(); actually calls the overriden method from the subclass.

    Also note that finalizer guardian should be a field of the class:

    public class Super {
        private final Object finalizerGuardian = new Object() {
                @Override
                protected void finalize() {
                    Super.this.doFinalize();
                }
        };
    
        private void doFinalize() {
            System.out.println("Super finalize");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If subclass in objective-c wants to override a super class's method, does it have
In Inheritance concept, i have a static method in super class and i am
Let's say I have a table that represents a super class, students . And
I have a super class like this: class Parent { public: virtual void Function(int
I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have a super-simple class representing a decimal # with fixed precision, and when
I have a generic class that is also a mapped super class that has
I have a super abstract class: @XmlSeeAlso({AndQuery.class, OrQuery.class, NotQuery.class, PropertyQuery.class, MultiQuery.class}) @XmlRootElement public abstract
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation
I have an abstract class called camera which PointCamera uses as its super class.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.