Infinite object?

Asked

Viewed 456 times

5

I ended up doing something that made me curious:

I created a Cao class to follow:

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 mistake:

Exception in thread "main" java.lang.StackOverflowError

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

  • 3

    This is the example of the error message that already gives the hint of where to find the answer... ;)

3 answers

6


This becomes something infinite correct?

Yes, you have prompted a new Cao type object at each instance of the Cao class, i.e., recursively you have created new and new Cao objects until the stack pops.

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

You can even, since you set a stop point. 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());
    }
}

Upshot:

Mike
Rex
Bilu

In the example above you will be creating a chained list of Cao-type objects according to the quantity set when instantiating your first Cao in your main method().

  • Got it! Thank you very much! I just have one more small question: When we instantiate an object for example Cao umCao = new Cao() Cao() is actually the same thing as Cao(1)? Is this an array of a single position? as long as you Cao umCao = new Cao(10) would be 10 variables one unit of type Cao?

  • @Brunoneuman a Cao will never be an array (in the examples above), what you have is a Cao type object that has a reference to another Cao type object, up to the limit of 10, hence the latter has a null reference. Example of new Cao(3) would be cao >> cao >> cao >> null. By the way, I think it is advisable to change the name variable caes for cao, because it just references only one, so it does not create confusion. It was clear?

  • I got it, I knew it was actually an object, I just used the same concept of array, which is actually the same by the look. Thanks again!

3

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

But you should never do and always start in the constructor because it will burst the stack because it is an infinite recursion. Whenever you call the new Classe() the builder 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.

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());
    }
}
  • Thanks again for the help =)

-1

You could do it this way:

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 more organized and concise way to achieve your goal, without losing control of the amount of instances you produce in the class.

Browser other questions tagged

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