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 7953267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:06:46+00:00 2026-06-04T03:06:46+00:00

If the constructor ends up with an exception, is the object created exactly the

  • 0

If the constructor ends up with an exception, is the object created exactly the same with a normal one?

class A {

    static A o;

    A() throws Exception {
        o=this;
        throw new Exception();
    }

    void f() { System.out.println("f(): entry."); };

    static public void main(String[]args ) {
        try {
         A o =new A();
        }
        catch (Exception e) {
              System.out.println("Exception: " + e);
        }

        A.o.f(); // Is it safe to use this object?
       }
}

This compiles and runs, and the output of this program is:

Exception: java.lang.Exception
f(): entry.
  • 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-06-04T03:06:48+00:00Added an answer on June 4, 2026 at 3:06 am

    If you catch the exception, the constructed object is never passed back to the caller, and the caller’s result variable is not set. However, the static variable would, in theory, be set, and I can’t see any reason why it would not be accessible.

    Note, however, that method verification will not allow you to store this until after the super constructor is called, so this is not a “back door” into otherwise protected objects.

    Since it’s your object, “safe” is up to you.

    [A little more detail: When the compiled version of your constructor is entered, the “raw” Java object is fully constructed — no additional work is needed by the system to make it valid. The super constructor has not been called yet, however, and it’s either up to you to explicitly make a super call or have the compiler insert the super call by default (which it will do at the start of the method if you don’t have an explicit call). The “bytecode verifier” in the JVM has some very strict rules about what can happen in a constructor prior to calling the super constructor, and storing this to a static would be a definite no-no — you’d get a VerifyError.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on constructor of the class. create or destroy new methods. I often requires
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
In one class, I have to call a constructor of another class that needs
In a C# constructor, that ends up with a call to this(...) , the
Constructor for PHP's exception has third parameter, documentation says: $previous: The previous exception used
I have a constructor (for an auto generated class) that has 255 paremeters. Using
If the constructor for Door looks like this: Door::Door(Doorknob doorknob) : m_doorknob(doorknob) { }
Is a public constructor in an abstract class a codesmell? Making the constructor protected
If a public constructor in an abstract class can only be called by their
I don't want constructor called. I am using placement new. I just want to

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.