Complete the code to declare a private field in a class.
public class Person { private String [1]; }
The field name is declared private to follow encapsulation best practices.
Complete the code to create a public getter method for the private field.
public class Person { private String name; public String [1]() { return name; } }
The getter method is named getName to provide controlled access to the private field name.
Fix the error in the setter method to properly set the private field.
public class Person { private String name; public void setName(String [1]) { name = [1]; } }
this keyword.The parameter should have a different name like newName to avoid confusion and correctly assign the value to the field.
Fill both blanks to complete the setter method using the this keyword.
public class Person { private String name; public void setName(String [1]) { [2].name = name; } }
this when parameter and field names are the same.this.The parameter is named name and the this keyword is used to refer to the field to avoid confusion.
Fill all three blanks to create a class with a private field and a getter following encapsulation best practices.
public class Car { private String [1]; public String [2]() { return [3]; } }
The private field is model. The getter method is getModel which returns the field model.