Type that has the name of the class

3

I found the following Java class:

public class MiClase {
    int informacion;
    MiClase siguiente;

    public MiClase(int valor) {
        informacion = valor;
        siguiente = null;
    }
}

There are native types in Java such as int and String. But in this class that I found, the author is creating a new type: MyClass , which is the name of the class.

  • When you create a class, you automatically create a data type that carries the name of that class? What is this called?
  • What kind of information can the following variable keep : int, bool, String?
  • In this place they ask the same as me, but in the answers they give, they talk about instantiating objects, and that is not what the question refers to.

    Can you please help me understand this? Please, I'm new to Java, so I would absolutely appreciate if you could answer so I can understand.

    Reformulate the question (but leaving the 1st formulation to see the changes)

    The original class is this:

    public class Nodo {
        int informacion;
        Nodo siguiente;
    
        public Nodo(int valor) {
            informacion = valor;
            siguiente = null;
        }
    }
    

    Around 9:00 minutes of this video tutorial is where I took the class, and there you can see how the stack type lists work. Now that I review the video again, the author does not refer to following as a variable, but as an object.

    This completely changes the doubt and the question. Because I understand that normally objects are created like this: Next node = new Node (); But in the video it turns out that an object can be created just like a variable is declared: Next node; But then I do not understand anything anymore, because the Node class is supposed to have 2 attributes: information and following . But how can one of its attributes be an instantiated object of the same class?

    Please excuse me for raising my doubts incorrectly, generally and even superficially. But I have tried to find answers, and I have not yet succeeded. That is why I have forced myself to ask for your help so that they can throw a little light on me. Thanks.

        
    asked by George Berkeley 03.01.2019 в 21:56
    source

    1 answer

    2
  • When you create a class, will you automatically create a data type named after that class?

    • It could be said that yes , the classes are a type of abstract data (ADT) if they are used to store information (you can create classes with pure methods and in that case you can not, in this regard, investigate the Utility Class or classes of prop methods in which it is prevented that instances are created with private constructors and other similar tricks).
  • What is this called?

    • Class, an instance or created object is of the type of the class that defines it that has the mold to create new objects. I do not know if there is a concept but I would call it a recursive definition since the object is defined in terms of itself. 1
  • What kind of information can the following variable keep: int, bool, String?

    • class (or type) information MiClase
  • To help you understand more about the program you include, I'll put some code on how to use the code you shared.

    public class MiClase {
        int informacion;
        MiClase siguiente;
    
        public MiClase(int valor) {
            informacion = valor;
            siguiente = null;
        }
    
        public static void main(String[] args) {
          MiClase miClase = new MiClase(1);
          System.out.println(miClase.informacion);
          miClase.informacion=0;
          System.out.println(miClase.informacion);
          miClase.siguiente = new MiClase(1);
          System.out.println(miClase.siguiente.informacion);
       }
    }
    

    The output of your program should be:

      

    1

         

    0

         

    1

    Explanation of the program:

    First, as you will know in most of the imperative languages, there is an execution stack, where the running processes are stored, so that when a subprocedure is executed, it will be returned to the next address where the execution was left.

    Because we define our class in hybrid mode (object with main method), the sample program has two frames in the stack or two independent sections, the first is our main, and the second He is our constructor.

    Using this knowledge the execution is done visually in the following way 2 :

    Now that if you need a deeper analysis, the args object that is of the String [] class or another form of string type is created at the beginning but it is never used. The main method first processes args and then begins to create instances or instances of type MiClase and associates one object with another using the next pointer called. Here the visualization 3 :

    As you will see on line 12 the modification of the values of an object from outside it can be a bit confusing, so I recommend that you appropriately encapsulate your class in the following way:

    Notes

    1 An example of a recursive definition in the semantic context of natural language as used in the computational domain is GNU, which means "GNU is not Unix", that is, GNU is defined only in terms of itself, I would dare to assert that currently only a human could understand without thinking about the rest of eternity that GNU has never been fully defined in the recursive definition that implies its name.

    2 Visualization created with cscircles

    3 Visualization created with Jeliot 3.7.2 (you must add the import import jeliot.io.*; at the beginning of the interactive demo)

        
    answered by 03.01.2019 в 23:55