Can you use a concept similar to keyword args for python in Java to minimize the number of accessor methods? -


i learn in python 3, minimize number of accessor methods class, can use dictionaries have 1 set of accessor methods follows:

def __init__(self, **kwargs):     self.properties = kwargs  def get_properties(self):     return self.properties  def get_property(self, key):     return self.properties.get(key, none) 

this seems useful , want apply similar in java. have been working on applications may have multiple attributes, , creating , keeping track of accessor methods can pain. there similar strategy use java?

use pattern if suits , fits best should exception rule in java - not appropriate.

best practice in java use getter , setter methods each variable. yes it's more code - - if lazy ide auto-generate them. it's there reason. many reasons. are;

  • promotes private variables
  • defines external interface other classes allowing class control , how set or variables
  • promotes coding practice establishing standard way deal object variables

the problem you'll encounter here porting pattern on due typing. python dynamically typed , java statically typed. so, in order use same pattern in java, have store values in string/object array , return them object or string. you'll have convert them sometime later. maybe if 'properties' strings okay. there exceptions rule, need know of rules in order break them well. if different types (string, int, customobject etc.) don't use same pattern use in python.