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

How to uniquely Identify windows forms?

.net winforms

My application needs to identify every form of every opened application. For example, if i press "Ctrl+f1" when i am in any application(lets say microsoft outlook), and if i am in the "new message"...

Azure VM downgrade from A8

azure virtual-machine

Is it possible to downgrade an Azure VM A8 (high compute) to a lower version like an A3? I keep getting the following error message when I try. I don't have an availability set setup. Thanks!

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.