1

I've been flustered over trying to figure out how to call a method from an instance of a class from a different class. For example:

public class Test1 
 {

    public static void makeSomeInst()
    {
        Test1 inst = new Test1();
        inst.fireTest();
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}

no problem with understanding the above, but what If I want to do something to inst from a class called Test2, how would I do that? The below example doesn't work:

public class Test2 
{
    public static void main(String[] args) 
    {
        Test1.makeSomeInst();
        inst.fireTest();
    }
}

And just to be extra clear, I get that I can call static references without instantiating, but I just want to know, in this specific case

What is the syntax to reference the test1 object called inst from the Test2 class?

12
  • Do exactly what you did before. Commented Dec 15, 2018 at 11:27
  • @Joe C That's rather unclear. I did Commented Dec 15, 2018 at 11:28
  • No, you only did half of what you did before. Before you had two lines. Your example only has one. Commented Dec 15, 2018 at 11:28
  • Like @JoeC said, you are forgetting Test1 inst = new Test1() in your Test2 class. Commented Dec 15, 2018 at 11:29
  • Are you suggesting that I make a new instance of Test1? I'm having difficulty understanding exactly what you mean. What I'm asking is how I refer to one that already exists created by a different class. Commented Dec 15, 2018 at 11:30

2 Answers 2

4

what If I want to do something to inst from a class called Test2, how would I do that?

First of all you have to teach the Test2 class what Test1 is.

public class Test2 
{
    public doSomething()
    {
        inst.fireTest();
    }

    public Test2(Test1 inst) 
    {
        this.inst = inst;
    }

    private Test1 inst;
}

Then teach the inst2 object what inst is.

public class Test1 
{

    public static void main(String[] args) 
    {
        Test1 inst = new Test1();

        Test2 inst2 = new Test2(inst); // <- new code

        inst2.doSomething();           // <- new code
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}

You only need one main to start the show. Flow of control can still pass through other objects. But at this point I wouldn't call these independent tests. I only used that name to match your code.

What you're looking at is something called reference passing. The fancy term for it is Pure Dependency Injection*. The basic pattern is to build an object graph in main. Once that's built call one method on one object to start the whole thing ticking.

In main you build every object that will live as long as your program does. What you wont find built here are objects that are born later, such as timestamps. A good rule of thumb is to build each of these long lived objects before doing any real work. Since they know about each other they can pass flow of control back and forth between them. There's a lot of power there and if not used well it can get confusing. Look into Architectural Patterns to help keep that simple.

The principle followed here is to separate use from construction. Following that allows you to easily change your mind about what talks to what in one place. It's nice when a design change doesn't force you to rewrite everything.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to save your instance somewhere. If Test1 should be a singleton, you can do:

public class Test1 
{
    private static Test1 instance;

    public static Test1 getInstance()
    {
        return instance == null ? instance = new Test1() : instance;
    }

    public static void main(String[] args) 
    {
        Test1 inst = getInstance();
        inst.fireTest();
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }
}

and in Test2:

public class Test2 
{
    public static void main(String[] args) 
    {
        Test1.getInstance().fireTest();
    }
}

//Edit

As I just learned from @Thomas S. comment, singletons are not a good solution. See @candied_orange's answer for a better implementation.

3 Comments

Thank you soooooooooooo much. I feel like I've asked this question so many time to so many people, but this is the only working example anyone has actually produced. You rock!
Please don't suggest a singleton. It is a quite bad pattern.
I am very interested in your comment. I am not familiar with singletons. Thank you for advising on best practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.