To define an object in a constructor in Serializable class











up vote
0
down vote

favorite












I have a JSON response like below image, and I have made a serializable class named as Project



JSON Response



In the image, I have two objects (emergency_contact, and secondary_owner)inside my an array of one object. I'm trying to figure out whether what to do in order to define the object, since I want that details to be present inside my constructor.



I have done this so far :



public class Project implements Serializable {
public int id;
public String name;
public String additional_information;
//Now what to do Emergency contact

public Project(int id, String name, String additional_information){

}
}


What I have thought of doing this, public EmergencyContact emergency = new EmergencyContact(param1, param2)



And make a new class named as EmergencyContact, and do a getter and setter for the params. But after doing this, I'm still confused, how would I define it my constructor?



I know I'm close, but I need some help on that. It would be highly appreciated. Thanks in advanced!










share|improve this question






















  • public Project(int id, String name, String additional_information, EmergencyContact emergencyContact) - same as any other field in constructor
    – Vladyslav Matviienko
    Nov 20 at 7:47










  • You have to define classes for both nested objects as well.
    – Ivan Kaloyanov
    Nov 20 at 7:47










  • Could you show it to me in respect of code please @IvanKaloyanov?
    – Alok
    Nov 20 at 7:47















up vote
0
down vote

favorite












I have a JSON response like below image, and I have made a serializable class named as Project



JSON Response



In the image, I have two objects (emergency_contact, and secondary_owner)inside my an array of one object. I'm trying to figure out whether what to do in order to define the object, since I want that details to be present inside my constructor.



I have done this so far :



public class Project implements Serializable {
public int id;
public String name;
public String additional_information;
//Now what to do Emergency contact

public Project(int id, String name, String additional_information){

}
}


What I have thought of doing this, public EmergencyContact emergency = new EmergencyContact(param1, param2)



And make a new class named as EmergencyContact, and do a getter and setter for the params. But after doing this, I'm still confused, how would I define it my constructor?



I know I'm close, but I need some help on that. It would be highly appreciated. Thanks in advanced!










share|improve this question






















  • public Project(int id, String name, String additional_information, EmergencyContact emergencyContact) - same as any other field in constructor
    – Vladyslav Matviienko
    Nov 20 at 7:47










  • You have to define classes for both nested objects as well.
    – Ivan Kaloyanov
    Nov 20 at 7:47










  • Could you show it to me in respect of code please @IvanKaloyanov?
    – Alok
    Nov 20 at 7:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a JSON response like below image, and I have made a serializable class named as Project



JSON Response



In the image, I have two objects (emergency_contact, and secondary_owner)inside my an array of one object. I'm trying to figure out whether what to do in order to define the object, since I want that details to be present inside my constructor.



I have done this so far :



public class Project implements Serializable {
public int id;
public String name;
public String additional_information;
//Now what to do Emergency contact

public Project(int id, String name, String additional_information){

}
}


What I have thought of doing this, public EmergencyContact emergency = new EmergencyContact(param1, param2)



And make a new class named as EmergencyContact, and do a getter and setter for the params. But after doing this, I'm still confused, how would I define it my constructor?



I know I'm close, but I need some help on that. It would be highly appreciated. Thanks in advanced!










share|improve this question













I have a JSON response like below image, and I have made a serializable class named as Project



JSON Response



In the image, I have two objects (emergency_contact, and secondary_owner)inside my an array of one object. I'm trying to figure out whether what to do in order to define the object, since I want that details to be present inside my constructor.



I have done this so far :



public class Project implements Serializable {
public int id;
public String name;
public String additional_information;
//Now what to do Emergency contact

public Project(int id, String name, String additional_information){

}
}


What I have thought of doing this, public EmergencyContact emergency = new EmergencyContact(param1, param2)



And make a new class named as EmergencyContact, and do a getter and setter for the params. But after doing this, I'm still confused, how would I define it my constructor?



I know I'm close, but I need some help on that. It would be highly appreciated. Thanks in advanced!







java android json serializable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 7:45









Alok

1651114




1651114












  • public Project(int id, String name, String additional_information, EmergencyContact emergencyContact) - same as any other field in constructor
    – Vladyslav Matviienko
    Nov 20 at 7:47










  • You have to define classes for both nested objects as well.
    – Ivan Kaloyanov
    Nov 20 at 7:47










  • Could you show it to me in respect of code please @IvanKaloyanov?
    – Alok
    Nov 20 at 7:47


















  • public Project(int id, String name, String additional_information, EmergencyContact emergencyContact) - same as any other field in constructor
    – Vladyslav Matviienko
    Nov 20 at 7:47










  • You have to define classes for both nested objects as well.
    – Ivan Kaloyanov
    Nov 20 at 7:47










  • Could you show it to me in respect of code please @IvanKaloyanov?
    – Alok
    Nov 20 at 7:47
















public Project(int id, String name, String additional_information, EmergencyContact emergencyContact) - same as any other field in constructor
– Vladyslav Matviienko
Nov 20 at 7:47




public Project(int id, String name, String additional_information, EmergencyContact emergencyContact) - same as any other field in constructor
– Vladyslav Matviienko
Nov 20 at 7:47












You have to define classes for both nested objects as well.
– Ivan Kaloyanov
Nov 20 at 7:47




You have to define classes for both nested objects as well.
– Ivan Kaloyanov
Nov 20 at 7:47












Could you show it to me in respect of code please @IvanKaloyanov?
– Alok
Nov 20 at 7:47




Could you show it to me in respect of code please @IvanKaloyanov?
– Alok
Nov 20 at 7:47












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Sure. You need to have a:



public class EmergencyContact implements Serializable {

public String name;
public String number;

public EmergencyContact(String name, String number){
// assign fields
}
}


and one for the owner:



public class EmergencyOwner implements Serializable {

public String name;
public String number;

public EmergencyOwner(String name, String number){
// assign the fields
}
}


then in your Project class you can add fields of these classes:



public class Project implements Serializable {
public int id;
public String name;
public String additional_information;
public EmergencyContact emergency_contact;
public EmergencyOwner emergency_owner;

public Project(int id, String name, String additional_information, EmergencyContact emergency_contact, EmergencyOwner emergency_owner){
// assign the fields here as well
}
}


that's it. If that's an answer to the question consider to delete this question as it is a duplicated on a 100% :)






share|improve this answer




























    up vote
    1
    down vote













    As a note, to be correctly from the point of clean code parameters, the fields should be private in a class, and use setters / getters to set/retrieve values from/to those fields.



    public class Project implements Serializable {
    private int id;
    private String name;
    private String additional_information;
    private EmergencyContact emergency_contact;
    private SecondaryOwner secondary_owner;

    public Project(int id, String name, String additional_information, EmergencyContact emergencyContact, SecondaryOwner secondaryOwner){
    this.id = id;
    this.name = name;
    this.additional_information = additional_information;
    this.emergency_contact = emergencyContact;
    this.secondary_owner = secondaryOwner;
    }
    }


    You will define the other two classes the same way. Now, you are probably confused about the constructor of EmergencyContact & SecondaryOwner classes.. You can device both default constructors (without parameters) and a custom one(with parameters to it, just as the one above). If you use the default constructor, make sure to set values to the fields in the object, as following :



    EmergencyContact emergencyContact = new EmergencyContact();
    emergencyContact.setName("the name");
    emergencyContact.setNumber("a number");


    then you can use this object in the constructor of Project class



    I hope it was clear enough, for any other clarifications feel free to ask.



    Happy coding <3






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388355%2fto-define-an-object-in-a-constructor-in-serializable-class%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Sure. You need to have a:



      public class EmergencyContact implements Serializable {

      public String name;
      public String number;

      public EmergencyContact(String name, String number){
      // assign fields
      }
      }


      and one for the owner:



      public class EmergencyOwner implements Serializable {

      public String name;
      public String number;

      public EmergencyOwner(String name, String number){
      // assign the fields
      }
      }


      then in your Project class you can add fields of these classes:



      public class Project implements Serializable {
      public int id;
      public String name;
      public String additional_information;
      public EmergencyContact emergency_contact;
      public EmergencyOwner emergency_owner;

      public Project(int id, String name, String additional_information, EmergencyContact emergency_contact, EmergencyOwner emergency_owner){
      // assign the fields here as well
      }
      }


      that's it. If that's an answer to the question consider to delete this question as it is a duplicated on a 100% :)






      share|improve this answer

























        up vote
        1
        down vote



        accepted










        Sure. You need to have a:



        public class EmergencyContact implements Serializable {

        public String name;
        public String number;

        public EmergencyContact(String name, String number){
        // assign fields
        }
        }


        and one for the owner:



        public class EmergencyOwner implements Serializable {

        public String name;
        public String number;

        public EmergencyOwner(String name, String number){
        // assign the fields
        }
        }


        then in your Project class you can add fields of these classes:



        public class Project implements Serializable {
        public int id;
        public String name;
        public String additional_information;
        public EmergencyContact emergency_contact;
        public EmergencyOwner emergency_owner;

        public Project(int id, String name, String additional_information, EmergencyContact emergency_contact, EmergencyOwner emergency_owner){
        // assign the fields here as well
        }
        }


        that's it. If that's an answer to the question consider to delete this question as it is a duplicated on a 100% :)






        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Sure. You need to have a:



          public class EmergencyContact implements Serializable {

          public String name;
          public String number;

          public EmergencyContact(String name, String number){
          // assign fields
          }
          }


          and one for the owner:



          public class EmergencyOwner implements Serializable {

          public String name;
          public String number;

          public EmergencyOwner(String name, String number){
          // assign the fields
          }
          }


          then in your Project class you can add fields of these classes:



          public class Project implements Serializable {
          public int id;
          public String name;
          public String additional_information;
          public EmergencyContact emergency_contact;
          public EmergencyOwner emergency_owner;

          public Project(int id, String name, String additional_information, EmergencyContact emergency_contact, EmergencyOwner emergency_owner){
          // assign the fields here as well
          }
          }


          that's it. If that's an answer to the question consider to delete this question as it is a duplicated on a 100% :)






          share|improve this answer












          Sure. You need to have a:



          public class EmergencyContact implements Serializable {

          public String name;
          public String number;

          public EmergencyContact(String name, String number){
          // assign fields
          }
          }


          and one for the owner:



          public class EmergencyOwner implements Serializable {

          public String name;
          public String number;

          public EmergencyOwner(String name, String number){
          // assign the fields
          }
          }


          then in your Project class you can add fields of these classes:



          public class Project implements Serializable {
          public int id;
          public String name;
          public String additional_information;
          public EmergencyContact emergency_contact;
          public EmergencyOwner emergency_owner;

          public Project(int id, String name, String additional_information, EmergencyContact emergency_contact, EmergencyOwner emergency_owner){
          // assign the fields here as well
          }
          }


          that's it. If that's an answer to the question consider to delete this question as it is a duplicated on a 100% :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:54









          Ivan Kaloyanov

          7492818




          7492818
























              up vote
              1
              down vote













              As a note, to be correctly from the point of clean code parameters, the fields should be private in a class, and use setters / getters to set/retrieve values from/to those fields.



              public class Project implements Serializable {
              private int id;
              private String name;
              private String additional_information;
              private EmergencyContact emergency_contact;
              private SecondaryOwner secondary_owner;

              public Project(int id, String name, String additional_information, EmergencyContact emergencyContact, SecondaryOwner secondaryOwner){
              this.id = id;
              this.name = name;
              this.additional_information = additional_information;
              this.emergency_contact = emergencyContact;
              this.secondary_owner = secondaryOwner;
              }
              }


              You will define the other two classes the same way. Now, you are probably confused about the constructor of EmergencyContact & SecondaryOwner classes.. You can device both default constructors (without parameters) and a custom one(with parameters to it, just as the one above). If you use the default constructor, make sure to set values to the fields in the object, as following :



              EmergencyContact emergencyContact = new EmergencyContact();
              emergencyContact.setName("the name");
              emergencyContact.setNumber("a number");


              then you can use this object in the constructor of Project class



              I hope it was clear enough, for any other clarifications feel free to ask.



              Happy coding <3






              share|improve this answer

























                up vote
                1
                down vote













                As a note, to be correctly from the point of clean code parameters, the fields should be private in a class, and use setters / getters to set/retrieve values from/to those fields.



                public class Project implements Serializable {
                private int id;
                private String name;
                private String additional_information;
                private EmergencyContact emergency_contact;
                private SecondaryOwner secondary_owner;

                public Project(int id, String name, String additional_information, EmergencyContact emergencyContact, SecondaryOwner secondaryOwner){
                this.id = id;
                this.name = name;
                this.additional_information = additional_information;
                this.emergency_contact = emergencyContact;
                this.secondary_owner = secondaryOwner;
                }
                }


                You will define the other two classes the same way. Now, you are probably confused about the constructor of EmergencyContact & SecondaryOwner classes.. You can device both default constructors (without parameters) and a custom one(with parameters to it, just as the one above). If you use the default constructor, make sure to set values to the fields in the object, as following :



                EmergencyContact emergencyContact = new EmergencyContact();
                emergencyContact.setName("the name");
                emergencyContact.setNumber("a number");


                then you can use this object in the constructor of Project class



                I hope it was clear enough, for any other clarifications feel free to ask.



                Happy coding <3






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  As a note, to be correctly from the point of clean code parameters, the fields should be private in a class, and use setters / getters to set/retrieve values from/to those fields.



                  public class Project implements Serializable {
                  private int id;
                  private String name;
                  private String additional_information;
                  private EmergencyContact emergency_contact;
                  private SecondaryOwner secondary_owner;

                  public Project(int id, String name, String additional_information, EmergencyContact emergencyContact, SecondaryOwner secondaryOwner){
                  this.id = id;
                  this.name = name;
                  this.additional_information = additional_information;
                  this.emergency_contact = emergencyContact;
                  this.secondary_owner = secondaryOwner;
                  }
                  }


                  You will define the other two classes the same way. Now, you are probably confused about the constructor of EmergencyContact & SecondaryOwner classes.. You can device both default constructors (without parameters) and a custom one(with parameters to it, just as the one above). If you use the default constructor, make sure to set values to the fields in the object, as following :



                  EmergencyContact emergencyContact = new EmergencyContact();
                  emergencyContact.setName("the name");
                  emergencyContact.setNumber("a number");


                  then you can use this object in the constructor of Project class



                  I hope it was clear enough, for any other clarifications feel free to ask.



                  Happy coding <3






                  share|improve this answer












                  As a note, to be correctly from the point of clean code parameters, the fields should be private in a class, and use setters / getters to set/retrieve values from/to those fields.



                  public class Project implements Serializable {
                  private int id;
                  private String name;
                  private String additional_information;
                  private EmergencyContact emergency_contact;
                  private SecondaryOwner secondary_owner;

                  public Project(int id, String name, String additional_information, EmergencyContact emergencyContact, SecondaryOwner secondaryOwner){
                  this.id = id;
                  this.name = name;
                  this.additional_information = additional_information;
                  this.emergency_contact = emergencyContact;
                  this.secondary_owner = secondaryOwner;
                  }
                  }


                  You will define the other two classes the same way. Now, you are probably confused about the constructor of EmergencyContact & SecondaryOwner classes.. You can device both default constructors (without parameters) and a custom one(with parameters to it, just as the one above). If you use the default constructor, make sure to set values to the fields in the object, as following :



                  EmergencyContact emergencyContact = new EmergencyContact();
                  emergencyContact.setName("the name");
                  emergencyContact.setNumber("a number");


                  then you can use this object in the constructor of Project class



                  I hope it was clear enough, for any other clarifications feel free to ask.



                  Happy coding <3







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 7:59









                  Ionut J. Bejan

                  435416




                  435416






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388355%2fto-define-an-object-in-a-constructor-in-serializable-class%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen