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

1

Follow 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)
}

Follow 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 question would be in p1 and p2 of the second example. Why are there two ways of instantiating? What is the best option to do this? Explain to me why the first form is that way and the second is the other way. Does it have to do with performance?

    
asked by anonymous 17.12.2015 / 14:43

2 answers

1

You should see the context. seem to be examples showing various ways of using the object.

I believe you are only 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 changed (with new identity). This is the same as saying x=1 and then say x = 2 .

The second one 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 , they're two different things.

Nothing to do with performance. Depends on what you want. I understand that these artificial examples often confuse rather than help.

If I had more context I could find another explanation, but I doubt it will escape it.

Make this example that may be more obvious to 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)
}

Follow 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)
}

You are now doing the 3 prints after instantiating the objects. As in the first example one object overlaps the other in the background, the 3 prints only print the final result. In the second example, because the data is preserved in 3 different variables, they can be printed independently at any time these variables are still active.

This type of construction is only for example, in real code nobody does this. Mostly put main() within class Pessoas .

    
17.12.2015 / 14:56
1

I believe your example was done to exemplify various ways of building an instance.

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

Already 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 inputs will be, in this way you will treat any incoming information.

For example, declare pessoa(){ ......} or pessoa(String firstName){.....} in this way, 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.     

17.12.2015 / 18:21