0

I want to set a value of a private field with double brackets initializer. The next is working

Customer customer1 = new Customer();
customer1.setFirstName("Nick");

This is also working:

Customer customer3 = new Customer("John", "Hammond", true, 80, "3/4/1978");

I also want to set the value of the private field with something like this. I know this is not the correct way. Is there any correct way to do this?

        Customer customer2 = new Customer()
        {
            {
               firstName = setFirstName("George");
            }
        };
PanosStack
  • 21
  • 5

1 Answers1

1

Thank you all for your answers and I apologise for Pascal Case names on my methods. The answer is the following (All the fields of my class are public except from firstName which is private):

           Customer customer2 = new Customer()
            {
                {
                    setFirstName("Tom");
                    lastName = "Cruz";
                    reliable = true;
                    weight = 70;
                    dateOfBirth = "2/3/1970";
                }
            };
PanosStack
  • 21
  • 5