Java send data to class where object was initialized

We have simple initialization: Example:

Class1
{
  public Class1
  {
   Class2 object = new Class2();
  }

   public SomeMethod(String anyString)
    {...}
}

So what is the best way to call SomeMethod in Class1 from Class2 and pass "anyString" value? Now I'm using custom events and interface. But maybe there is better, more rational way to do this?

Answers


But maybe there is better, more rational way to do this?

Yep.

Class1
{
    public Class1()
    {
        Class2 object = new Class2(this);
    }

   public SomeMethod(String anyString)
   {...}
}

Class2
{
    public Class2(Class1 parent)
    {
         parent.SomeMethod("A String value");
    }
}

Explanation

Simply pass an instance of the parent object into the child class. The child class can call the desired method directly.

Java Naming Conventions

The convention states that methods start with a lower case letter.

SomeMethod -> someMethod

Need Your Help

Focus lost on partial postback with UserControls inside UpdatePanel

asp.net user-controls focus updatepanel setfocus

I have a bunch of user controls on a form inside an AJAX UpdatePanel, containing a couple of controls including a TextBox.

pass view from one activity to another

java android view android-activity graph

I'm trying to pass a view from one activity to another activity.