How to return the instance of a class in Java [closed]

0

Does anyone know how to return an instance of a class in Java?

I have a class Server, I instantiate the class thus turning an object. And u want to return an instance to another main method, without instantiating another object of that class. Got better understood?

    
asked by anonymous 06.02.2015 / 21:46

3 answers

2

If I understand, it's quite 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
}

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 are just going to return the instance and do nothing else in the method, it is better to create the instance directly in the variable or where it will be used without creating a method for it.

    
06.02.2015 / 21:55
1

Use the Singleton design pattern.

  • Make your builder private.
  • Store an instance of your class in a private static field.
  • Always return the same instance (the one in the static private field) in the static method responsible for returning the instance to you. Thus:
  • 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.
    }
    
        
    06.02.2015 / 23:36
    0
      

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

    Yes, just do this:

    return instancia;
    
      

    I'll try to explain it better,

         

    I have a class Server, I instantiate the class thus turning an object I WANT TO RETURN THIS INSTANCE to another method main , WITHOUT INSTANCING ANOTHER object of that class. Got better understood?

    No, it did not get better understood. It's very confusing.

    The most obvious suggestion would be to use the singleton. But ...

      

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

      

    Without being singleton.

    Ok, let's look at what we have:

    • There is some method that should return to main an instance of class Server . This also means that this method was invoked by main itself.
    • This method should always return the same instance.
    • Class Server is not a singleton.

    So, does something like this solve it?

    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 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;
    }
    
        
    06.02.2015 / 23:53