What is the difference between instantiation in the same variable or in 3 different variables?

Asked

Viewed 152 times

2

Follows part one:

class Pessoa{

    String firstName
    String lastName
    int age
    def address

    static main(args) {
      def p = new Pessoa()

      p.setFirstName("Lars")

      p.lastName = "Vogel"
      p.address = "Homestreet 3"
      println(p.firstName + " " + p.lastName);

      println ""


      p = new Pessoa(firstName: "Peter", lastName:"Mueller");
      println(p.firstName + " " + p.lastName);

      println ""

      p = new Pessoa(firstName: "Aline", lastName: "Gonzaga")

      println (p.firstName+" "+ p.lastName)
}

Follows part two:

class Pessoa{

    String firstName
    String lastName
    int age
    def address

    static main(args) {
      def p = new Pessoa()

      p.setFirstName("Lars")

      p.lastName = "Vogel"
      p.address = "Homestreet 3"
      println(p.firstName + " " + p.lastName);

      println ""


     def p1 = new Pessoa(firstName: "Peter", lastName:"Mueller");
      println(p1.firstName + " " + p1.lastName);

      println ""

     def p2 = new Pessoa(firstName: "Aline", lastName: "Gonzaga")

      println (p2.firstName+" "+ p2.lastName)
}

My doubt would be on p1 and p2 of the second example. Why are there these two forms of instantiation? What is the best option to do this? Explain to me why the first way is like that and the second way is the other way. It has to do with performance?

2 answers

1


I would need to see the context. appear to be examples showing various forms of object use.

I believe it’s just demonstrating in the first how to use the same variables to store different objects - not at the same time, one overlaps the other. That is, there is only one data that has its value exchanged (with new identity). That is the same as saying x=1 and then say x = 2.

The second shows that objects can be stored in different variables, thus maintaining their state in each of them. There are 3 different data. It’s the same as saying x = 1 and y = 2, are two different things.

Nothing to do with performance. It depends on what you want. I understand that these artificial examples often confuse you more than they help.

If I had more context I could find another explanation, but I doubt it will run away from this.

Make this example that perhaps it becomes more obvious the change:

class Pessoa {
    String firstName
    String lastName
    int age
    def address

    static main(args) {
      def p = new Pessoa()
      p.setFirstName("Lars")
      p.lastName = "Vogel"
      p.address = "Homestreet 3"
      p = new Pessoa(firstName: "Peter", lastName:"Mueller");
      p = new Pessoa(firstName: "Aline", lastName: "Gonzaga")
      println(p.firstName + " " + p.lastName);
      println ""
      println(p.firstName + " " + p.lastName);
      println ""
      println (p.firstName+" "+ p.lastName)
}

Follows part two:

class Pessoa {
    String firstName
    String lastName
    int age
    def address

    static main(args) {
      def p = new Pessoa()
      p.setFirstName("Lars")
      p.lastName = "Vogel"
      p.address = "Homestreet 3"
      def p1 = new Pessoa(firstName: "Peter", lastName:"Mueller");
      def p2 = new Pessoa(firstName: "Aline", lastName: "Gonzaga")
      println(p.firstName + " " + p.lastName);
      println ""
      println(p1.firstName + " " + p1.lastName);
      println ""
      println (p2.firstName+" "+ p2.lastName)
}

I put in the Github for future reference.

Now he’s making all three impressions after instantiating the objects. As in the first example one object superimposes the other on the background to the 3 prints only print the final result. In the second example, as the data is preserved in 3 different variables, they can be printed independently at any time that these variables are still active.

This type of construction only serves for example, in real code nobody does it. Mainly put the main() within the class Pessoas.

  • Lars Vogel Peter Mueller Aline Gonzaga This is printed. the two forms print alike, how can one superimpose the other? , that part of the state as such? I did not understand...

  • The variable p is storing the Lars, then she stores the Peter and then it goes on to store the Aline. It’s like you say x = 1 and then say x = 2. It’s that simple.

  • but what about printing? Lars Vogel Peter Mueller Aline Gonzaga .

  • You don’t know anything about programming then? He’s having people’s names printed up. I don’t know what the question is. For example, it helps to understand the use of variables.

  • If x=1 and then x=2 would not print 2?

  • I think I understand, the secret is in the position of the println.

  • If I do x = 1 println x x=2 println x Print 1 and 2. : )

  • That’s right. Programming follows a stream. Order is fundamental. This is the basis of every code.

  • right! I get it.

Show 4 more comments

1

I believe your example was made to exemplify several ways to build an instance.

When you instance an object, it automatically invokes a constructor method, which has not been declared. If you instantiate an object with Person(); the person() constructor method will be invoked; it will only initialize variables with empty values.

When instantiating with Person(firstName: "Peter"); you invoke a constructor method that accepts a parameter.

You can control this by declaring a constructor method and specifying what your entries will be, this way you will handle the information that arrives.

for example declare pessoa(){ ......} or pessoa(String firstName){.....} thus, at the time the object is instantiated, the language itself identifies which of the two constructor methods will be called, based on the type of parameter provided.

Browser other questions tagged

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