Java Class.getField(String name)

Syntax

Class.getField(String name) has the following syntax.

public Field getField(String name)  throws NoSuchFieldException ,   SecurityException

Example

In the following code shows how to use Class.getField(String name) method.


/*ww w  .  ja  v a2 s. co  m*/
import java.lang.reflect.Field;

public class Main {
  public static void main(String[] args) throws Exception {

    MyClass c = new MyClass();
    Class cls = c.getClass();

    Field sField = cls.getField("string1");
    System.out.println("Public field found: " + sField.toString());
  }
}

class MyClass {

  public MyClass() {
  }

  public MyClass(String string1) {
    this.string1 = string1;
  }

  public String string1 = "tutorialspoint";
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable