Passing "this" to a constructor, bad form?

Posted on 2003-03-20
Medium Priority
Last Modified: 2008-03-06
I just starting working for a new company after a 9 month period of twidling my thumbs.  They have a poorly created class hierarchy.  I'm trying to clean it up but each class uses so much crap from every other class it's difficult.  One type of class in particular is xxxCmdThread.  There are about 20 seperate class file with different names that use identical code, I'm trying to collapse them into one.  The problem is that each one references pieces of it's creator directly.  These CmdThread classes really only handle shutdowns and do some logging (using the creators log reference).  The CmdThread also changes the member var shutdown flag to true and then reads it's JavaSpace.  Would it be bad form to just pass "this" to the CmdThread and let it invoke these directly or should I look to find another way?  

Jan Gonsalves
Question by:Tridecaphi
LVL 16

Accepted Solution

imladris earned 300 total points
ID: 8174486
Sounds fine to me. At worst you're looking for the lesser evil. Having 20 nearly identical classes is certainly bad code. And passing "this" to a class so that it can invoke methods of another class is not in and of itself a violation of OOP principles. Classes, of necessity call each other methods all the time. The goal in Object Oriented Code is to ensure that the objects call each other only through a well defined interface, which hides their internal details from each other (so that their internals can change without unduely destabilizing the system).
LVL 35

Expert Comment

ID: 8174488
>  The problem is that each one references pieces of it's creator directly.

  What exactly do you mean here? Do you mean there is a Factory class that creates instances of objects and each one of them is using the variables of a superclass?

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question