Skip to main content
Post Reopened by Alex Guteniev, Ari, Drew Dormann
English
Source Link
khelwood
  • 59.7k
  • 14
  • 91
  • 116

Passing reference of this class instance in it'sits constructor to other class instances

In Java, I can write something like this:

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

This way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {
    
    public static void main(String args) {
       // somehow this seems to be like a container for all objects; ApplicationContext?
       Parent parentInstance = new Parent();

       parentInstance.getBrother().annoySister();
       parentInstance.getSister().parentsName();
    }
}

Q. How can this be passed to other classes, when Parent is being created as it'sits constructor is being executed?

Passing reference of this class instance in it's constructor to other class instances

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

This way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

Q. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

Passing reference of this class instance in its constructor to other class instances

In Java, I can write something like this:

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

This way all the objects created in Parent's class are available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {
    
    public static void main(String args) {
       // somehow this seems to be like a container for all objects; ApplicationContext?
       Parent parentInstance = new Parent();

       parentInstance.getBrother().annoySister();
       parentInstance.getSister().parentsName();
    }
}

Q. How can this be passed to other classes, when Parent is being created as its constructor is being executed?

Keeping the focus of the question to on one subject
Source Link

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

I created this code with intention that thisThis way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

If there is a new class DatabaseLookup instantiated in Parent's constructor, all the methods to lookup things from database are accessible to both Brother and Sister class; parent.getDatabaseLookup().getUsers();

Q. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

I created this code with intention that this way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

If there is a new class DatabaseLookup instantiated in Parent's constructor, all the methods to lookup things from database are accessible to both Brother and Sister class; parent.getDatabaseLookup().getUsers();

Q. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

This way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

Q. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

Focusing on a single question
Added to review
Source Link

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

Q1. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

I created this code with intention that this way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

The Parent's class looks to me like container. It contains instances of all classes being created and provides a way to access this to all those classes. So for example, ifIf there is a new class DatabaseLookup; instantiated in Parent's constructor, all the methods to lookup things from database are accessible to both Brother and Sister class; parent.getDatabaseLookup().getUsers();

Q. Q2 Is this some kind of design patternHow can this be passed to other classes, when Parent is being created as it's constructor is being executed? Some detail explanation would really help me understand. Thanks

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

Q1. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

I created this code with intention that this way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

The Parent's class looks to me like container. It contains instances of all classes being created and provides a way to access this to all those classes. So for example, if there is a new class DatabaseLookup; all the methods to lookup things from database are accessible to both Brother and Sister class; parent.getDatabaseLookup().getUsers();

Q2 Is this some kind of design pattern? Some detail explanation would really help me understand. Thanks

In Java, I can write something like this

public class Brother {
   private final Parent parent;
   
   public Brother(Parent parent) {
      this.parent = parent;
   }

   public void annoySister() {
       parent.getSister().annoy();
   }

}

public class Sister {
   private final Parent;
   
   public Sister(Parent parent) {
      this.parent = parent;
   }

   public void annoy() {
      System.out.println("I am annoyed");
   }

   public void parentsName() {
      parent.getName();
   }
}
    
public class Parent {
   private final Brother brother;
   private final Sister sister;
   private final String name;

   public Parent() {
      this.name = "Johnny";
      this.brother = new Brother(this);
      this.sister = new Sister(this);
   }
   // getters
}

I created this code with intention that this way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).

public class Main {

public static void main(String args) {
   // somehow this seems to be like a container for all objects; ApplicationContext?
   Parent parentInstance = new Parent();

   parentInstance.getBrother().annoySister();
   parentInstance.getSister().parentsName();
}

If there is a new class DatabaseLookup instantiated in Parent's constructor, all the methods to lookup things from database are accessible to both Brother and Sister class; parent.getDatabaseLookup().getUsers();

Q. How can this be passed to other classes, when Parent is being created as it's constructor is being executed?

Post Closed as "Needs more focus" by Progman, Sweeper, vimuth
Keeping the focus on two questions only.
Source Link
Loading
Keeping the focus on two questions only.
Source Link
Loading
Source Link
Loading