Infinite object?

5

I ended up doing something that made me curious:

I created a following Cao class:

public class Cao {

    String nomeDoCao = null;
    Cao caes = new Cao();

    public void setName(String name) {

        nomeDoCao = name;
    }

    public String getName() {

        return nomeDoCao;
    }
}

public class Main {

    public static void main(String[] args) {

        Cao umCao = new Cao();

        umCao.setName("Mike");
        umCao.caes.setName("Rex");
        umCao.caes.caes.setName("Totoh");
        umCao.caes.caes.caes.caes.caes.setName("Bilu");

        System.out.println(umCao.getName());
        System.out.println(umCao.caes.getName());
        System.out.println(umCao.caes.caes.caes.caes.caes.getName());
    }
}

I get this error:

Exception in thread "main" java.lang.StackOverflowError

Does this become something infinite right? When programming there is the possibility of instantiating an object in the object itself? Or should NEVER do that?

    
asked by anonymous 22.07.2014 / 02:48

3 answers

6
  

Does this become something infinite right?

Yes, you instantiated a new object of type Cao at each instantiation of the Cao class, that is, recursively you created new and new Cao objects until you popped the stack.

  

When programming, is it possible to instantiate an object in the object itself? Or should NEVER do that?

You can even as long as you set a breakpoint. Example:

class Cao {
    String nomeDoCao = null;
    Cao caes;
    public Cao(int numeroCao) {
        if(numeroCao > 0) {
            caes = new Cao(numeroCao - 1);
        }
    }
    public void setName(String name) {
        nomeDoCao = name;
    }
    public String getName() {
        return nomeDoCao;
    }
}

class Main {
    public static void main(String[] args) {
        Cao umCao = new Cao(10);
        umCao.setName("Mike");
        umCao.caes.setName("Rex");
        umCao.caes.caes.setName("Totoh");
        umCao.caes.caes.caes.caes.caes.setName("Bilu");
        System.out.println(umCao.getName());
        System.out.println(umCao.caes.getName());
        System.out.println(umCao.caes.caes.caes.caes.caes.getName());
    }
}

Result:

  

Mike
  Rex
  Bilu

In the example above you will be creating a threaded list of objects of type Cao according to the amount set at the time of instantiating your first Cao in your main () method.

    
22.07.2014 / 03:13
3

You can instantiate an object of the same class within an object of that same class and in some cases it is a good solution due to the recursiveness of some problems.

However you should never do and always start in the constructor because this will pop the stack because it is infinite recursion. Whenever you call the new Classe() the constructor is called.

public class Cao {
    String nomeDoCao = null;
    Cao caes = new Cao();
}

public class Main {
    public static void main(String[] args) {
        Cao umCao = new Cao();
    }
}

To show a valid example:

Hierarchy.java

public class Hierarquia{

    private String posicao; 
    private Hierarquia subPosicao;
    public Hierarquia(String nivel){
        posicao = nivel;
    }
    public Hierarquia getSubPosicao() {
        return subPosicao;
    }

    public void setSubPosicao(Hierarquia subPosicao) {
        this.subPosicao = subPosicao;
    }
    public String getPosicao() {
        return posicao;
    }
}

Main.java

public class Main {

    public static void main(String[] args) {

        Hierarquia chefe = new Hierarquia("chefe");
        Hierarquia gerente = new Hierarquia("gerente");
        Hierarquia programador = new Hierarquia("programador");
        chefe.setSubPosicao(gerente);
        gerente.setSubPosicao(programador);
        System.out.println(programador.getPosicao());
        System.out.println(chefe.getSubPosicao().getSubPosicao().getPosicao());
    }
}
    
22.07.2014 / 03:08
-1

You could do this as follows:

public class Cao {

String nome;
List<Cao> caes;

public Cao(String nome){
   this.nome = nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getNome() {
    return nome;
}

public List<Cao> getCaes(){
    return caes;
}

public void adicionarCao(String nome){
   if(caes == null){
      caes = new ArrayList<Cao>();
   }
   caes.add(new Cao(nome));
}

}

public class Main {

public static void main(String[] args) {

    Cao umCao = new Cao("Mike");

    umCao.adicionarCao("Rex);
    umCao.adicionarCao("Totoh");
    umCao.adicionarCao("Bilu");

    System.out.println(umCao.getNome());
    System.out.println(umCao.getCaes().get(0).getNome());
    System.out.println(umCao.getCaes().get(1).getNome());
    System.out.println(umCao.getCaes().get(2).getNome());
}

}

So you have a more organized and concise way of achieving your goal, without losing control of the number of instances you produce in the class.

    
24.07.2014 / 18:03