What is the Attribute.getName method in Java?

The Attribute class denotes an MBean attribute. The MBean server and other objects use this class to get and set attributes values.

The Attribute class is present in the javax.management.Attribute package.

What is the getName method of the Attribute class?

The getName method can be used to get the name of the attribute object.

Syntax


public String getName()

Arguments

This method doesn’t take an argument.

Returns

This method returns a string representing the key of the Attribute object.

Code

The code below shows how to use the getName method.

import javax.management.Attribute;
class AttributeGetNameExample {
public static void main( String args[] ) {
Attribute attr = new Attribute("Website_Name", "Educative");
System.out.println( "Attribute name is: " + attr.getName());
}
}

Explanation

In the code above:

  • Line 1: We imported the Attribute class.

import javax.management.Attribute

  • Line 4: We created an Attribute object with the name attr. For this object, we set the attribute name as Website_Name and the attribute value as Educative.

Attribute attr = new  Attribute("Website_Name", "Educative");

  • Line 5: We called the getName method on the attr object and printed the value returned.

attr.getName(); //Website_Name

Free Resources