Class FieldPosition in Java

Class FieldPosition in Java

FieldPosition objects are optionally passed to the format() methods of the Format class and its subclasses to return additional information about the formatting that has been performed.

The getBeginIndex() and getEndIndex() methods of this class are used to return the starting and ending character positions of some field of the formatted string. The integer value passed to the FieldPosition() constructor specifies what field of the returned string should have its bounds returned.

The NumberFormat and DateFormat classes define various constants (which end with the string _FIELD) that can be used here. Typically, this bounds information is useful for aligning formatted strings in columns–for example, aligning the decimal points in a column of numbers.


The structure of the class FieldPosition is given by

public class java.text.FieldPosition extends java.lang.Object {
// Public Constructor
public FieldPosition(int field);
// Public Instance Methods
public int getBeginIndex();
public int getEndIndex();
public int getField();

The FieldPosition class encapsulates information about fields in formatted output. The fields in a particular type of formatted output are identified by constants. A FieldPosition object contains its field type and the field’s position within the formatted output. The field position is specified by the index of the first character in the field and the index of the last character in the field.

We can use a FieldPosition object to find the position of a particular field in a formatted string.
Consider the following code:

NumberFormat nf = NumberFormat.getInstance();
StringBuffer sb = new StringBuffer();
FieldPosition fp = new FieldPosition(NumberFormat.FRACTION_FIELD);
nf.format(-1234.56, sb, fp);
System.out.println(new String(sb));
System.out.println("FRACTION_FIELD goes from " + fp.getBeginIndex()+ " to " + fp.getEndIndex() + ".");

This code produces the following output:
FRACTION_FIELD goes from 7 to 9.

See also  Common Exception Classes in Java With Example

The details of the class structure are given as follows:

public FieldPosition(int field);

public FieldPosition(int field) constructor creates a FieldPosition object that represents the given field.

field – A field constant.

public int getBeginIndex();

public int getBeginIndex() method returns the beginning index of the field that is represented by this FieldPosition.

This method returns the beginning index.

public int getEndIndex();

public int getEndIndex() method returns the ending index of the field that is represented by this FieldPosition.

This method returns the ending index of this FieldPosition.

public int getField();

public int getField() method returns the field constant of this FieldPosition.
This method returns the field constant of this FieldPosition.

Apart from these FieldPosition class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *