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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:17:11+00:00 2026-05-31T10:17:11+00:00

Today I met an interesting situation in a huge project. A class has several

  • 0

Today I met an interesting situation in a huge project.
A class has several constructors which calls each other with this(), and at the end will call the init(), build() and so on. I wanted to set a flag and after that call the this() and the whole cumbersome process, but calling this() it should be the first.

How can I modify a code inside this class, without modifying the Contructor headers and set the flag? 🙂

I know it sounds like hackish, and maybe this aren’t learned at schools, that’s why is interesting at least for me. For others can be usefull too in some cases.

Here is a basic example, I did a few modification to simulate the real problem, the init() method.
http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

public class Rectangle {
    private int x, y;
    private int width, height;
    private boolean flag;

    public Rectangle() {
      // execute code here, before this(), how?  -set the flag true for eg.
      this(0, 0, 0, 0);
    }
    public Rectangle(int width, int height) {
       // execute code here to, something different as above, before this(), how?   
       this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        init();
    }
    private void init(){
      if(flag){
      ... do something new, else or different as the original, maybe return, even exit too
      }

      ... do something... the old code
    }
}

I got 1 easy implementation, but until I wrote this question I got the second one too.

My important questions are unanswered, but I hope this will be and I can accept somebody’s answer, who want to build up reputation.

The init() method can’t be coded twice or the logic of the code written in 2 places, because it is not a good programming paradigm, and that invokes maybe a few millions line of the code.

————- Edit added —————-

There is a way to known from which constructor was called: the full parametrized one or anything else with this() -I hope it gives more idea:

    public Rectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;

            try {
                throw new RuntimeException("Hacking use a lot of imagination");
            } catch (Exception ex) {
                StackTraceElement[] stackTraces = ex.getStackTrace();
                // the first element is just above, no reason to check
                String thisClassName = getClass().getName();

                if (stackTraces[1].getClassName().equals(thisClassName)) {
                    if (stackTraces[1].getMethodName().equals("<init>")) {
                        flag = true;
                    }
                }
            }

            init();
        }

        private void init() {
            if (flag) {
                System.out.println("\"... do something new, else or different as the original, maybe return, even exit too\"");
            }

            System.out.println("\"... do something... the old code");

        }

———————– Edit adding solution 1 -a very simple case

    public Rectangle() {
        // execute code here, before this(), how?  -set the flag true for eg.
        this(doVeryBanalHack(0, false), 0, 0, 0);
    }

    public Rectangle(int width, int height) {
        // execute code here to, something different as above, before this(), how?   
        this(doVeryBanalHack(0, false), 0, width, height);
    }

    public Rectangle(int x, int y, int width, int height) {
        this.x = doVeryBanalHack(x, true);
        this.y = y;
        this.width = width;
        this.height = height;
             // TODO deal with concurrency if you are in multithreaded environment, otherwise is done
           this.flag = nextValueOfFlag;
           init();
....}

   private static boolean nextValueOfFlag;

   private static int doVeryBanalHack(int retValue, boolean flagValue) {
           System.out.println("\"execute code here, before this() it is too simple, it is banal static function\");
         // TODO deal with concurrency if you are in multithreaded environment
        nextValueOfFlag = flagValue;
   }

the reason why in a huge projet can’t changed the function signatures are ( one of them) the dynamic loading, and reflection usage: http://tutorials.jenkov.com/java-reflection/constructors.html
http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html

http://www.java-forums.org/java-lang/7896-object-reflection-invoking-constructor-parameters.html
Some IDE are enough intelligent to find refferences to this class even with Class.forName(“java.awt.Rectangle”), if you have the sources, but if they are in third party libraryes ( plugins), than probably not. A licence checking routine want to hide himself and the developper is a bit experienced will split the “Rectangle” as “Rect” + “tagle” , or even more complex (decodeString) ( but this is enough. Highly doubt your super intelligent editor can find references than 🙂

The next solution can be with reflection ( that was the second one solution which I got typing here and I wrote above) -nobody mentioned yet

  • 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-31T10:17:13+00:00Added an answer on May 31, 2026 at 10:17 am

    In this case, I’d go for a private constructor with an extra argument.

    [...]
    public Rectangle() {
        this(true, 0, 0, 0, 0);
    }
    public Rectangle(int width, int height) {  
        this(false, 0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this(true, x, y, width, height);
    }
    private Rectangle(boolean flag, int x, int y, int width, int height) {
        this.flag = flag;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        init();
    }
    [...]
    

    As a general rule, it’s generally good to avoid doing to much with constructors.

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

Sidebar

Related Questions

Today I met some unexpected behavior on doctrine (1.2). Situation I've a Document class,
I'm applying UnitTest just for a while, and today I met something very strange.
Today I was met the following code block: #include <iostream> using namespace std; char
Today I came across this question: you have a code static int counter =
Today I encountered this article about decimal expansion and I was instantaneously inspired to
Today, I got completely surprised when I saw that a global variable has undefined
Today I had an interview on which I asked candidate quite usual and basic
Today I had to define the WIN32_MEAN_AND_LEAN preprocessor macro in a native C++ project
Today, reading Servlet 3.0 specification, I've come across a sentence: We emphasize that this
I've always programmed in Java, which is probably why I'm so confused about this:

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.