Java BeanUtils Basic Property Access - Java Bean Utils

What is Java BeanUtils Basic Property Access?

Description

You can access the basic properties by using the following ways:

  • Simple Property
  • Indexed Property
  • Mapped Property

Simple Property

You can get and set the simple property values by using the below API signatures:

  • PropertyUtils.getSimpleProperty(Object, String)
  • PropertyUtils.SetSimpleProperty(Object, String, Object)

Parameters:
Object: It is a bean object that specifies the bean property to be extracted.
String: It is a string name that specifies the name of the property to be extracted.

Indexed property

You could use options for creating indexed properties; first choice is building the subscript into property name and second option is defining the subscript in a separate argument to name the method.

The indexed properties may be get and set by the use of the below methods:

  • PropertyUtils.getIndexedProperty(Object, String)
  • PropertyUtils.getIndexedProperty(Object, String, int)
  • PropertyUtils.setIndexedProperty(Object, String, Object)
  • PropertyUtils.setIndexedProperty(Object, String, int, Object)

Parameters:
Object: It is a bean object that specifies the bean property to be extracted.
String: It is a string name that specifies the name of the property to be extracted.
Int: It sets an index of the property value.
Object: It specifies the value for an indexed property element.

Mapped Property

You can get and set the mapped property values by means of using the below API signatures. if you have any extra argument, then it can be written inside parentheses as ("(" and ")") instead of the use of square brackets.

  • PropertyUtils.getMappedProperty(Object, String)
  • PropertyUtils.getMappedProperty(Object, String, String)
  • PropertyUtils.setMappedProperty(Object, String, Object)
  • PropertyUtils.setMappedProperty(Object, String, String, Object)

Parameters:
Object: It is a bean object that specifies the bean property to be extracted.
String: It is a name of the property value that should be set for Mapped property.
String: It defines the key of the property value to be set.
Object: It specifies the value of property to be set.

Example

The below example demonstrates use of above properties in the beanUtils:

Now we will create one more class called MyBean.java for the bean class:

OutPut

Let's carry out the following steps to see how above code works:

  • Save the above first code as BeanUtilsPropertyDemo.java.
  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Java BeanUtils - Basic Property Access

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Bean Utils Topics