How to return the instance of a class in Java

Asked

Viewed 3,183 times

0

Can anyone tell me how to return an instance of a class in Java?

I have a Server class, I urge the class so turning into an object. And u want to return an instance to another method main, without instantiating another object of that class. It was better understood?

  • 2

    Return where to? Which class?

  • For your caller, in this case it would be a main method and I was going to assign it to a reference of that class, I just wanted the instance same.

  • 2

    <troll> Use return. </troll> Singleton, that would be it?

  • You need to give more information than you are doing to have a more accurate answer. I might even try to answer just with this but I probably won’t help you:

  • Other than Singleton.

3 answers

3

Use the Singleton project pattern.

  1. Make your builder private.
  2. Store an instance of your class in a private static field.
  3. Always return the same instance (the one in the private static field) in the static method responsible for returning the instance to you. Like this:
public final class Server {
    private static final Server INSTANCIA = new Server();

    // Outros campos...

    private Server() {
        // O que você quiser.
    }

    public static Server instancia() {
        return INSTANCIA;
    }

    // Outros métodos.
}
  • 1

    It was the first (and only) thing that came to mind: Singleton.

  • He says it can’t be Singleton.

  • @That’s right, I saw it later, because it was put in a comment rather than being put in the question. Because of that, I created another answer.

2

If I understand, it’s something very simple:

public static void main (String[] args) {
    Server instancia = metodo(); //a variável instancia terá uma instância de Server
}
public static Server metodo() {
    return new Server(); //só faz sentido se fizer outras coisas no método
}

I put in the Github for future reference.

Another way to get the same thing cleaner:

public static void main (String[] args) {
    Server instancia2 = new Server(); //mas é mais fácil fazer isto se não vai fazer outras coisas
}

If you will only return the instance and do nothing else in the method, it is better to create the instance directly in the variable or where to use without creating a method for this.

0

Can anyone tell me how to return an instance of a java class???

Yeah, just do that:

return instancia;

I’ll try to explain it better,

I have a Server class, I urge the class to become an object I WANT TO RETURN THIS INSTANCE TO another method main, WITHOUT INSTANTIATING ANOTHER object of that class. It was better understood?

No, it’s not better understood. It’s super confusing.


The most obvious suggestion would be to use Singleton. But...

For your caller, in this case it would be a main method and I was going to assign it to a reference of that class, I just wanted the instance same.

Other than Singleton.


Okay, let’s look at what we have:

  • Is there any method that should return to main an instance of the class Server. This also means that this method was invoked by main.
  • This method should always return the same instance.
  • The class Server is not an English.

So does something like this solve?

private static final Server instancia = new Server();

public static void main(String[] args) {
    Server instancia = obterInstancia();
}

private static Server obterInstancia() {
    return instancia;
}

Or maybe you want to instantiate the class Server lazily (Lazy):

private static Server instancia = null;

public static void main(String[] args) {
    Server instancia = obterInstancia();
}

private static synchronized Server obterInstancia() {
    if (instancia == null) instancia = new Server();
    return instancia;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.