Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Field getType() method in Java with Examples
Next article icon

Field getInt() method in Java with Examples

Last Updated : 26 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

The getInt() method of java.lang.reflect.Field used to get the value of int which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type int via a widening conversion. When a class contains a static or instance int field and we want to get the value of that field then we can use this method to return the value of Field.

Syntax:

  public int getInt(Object obj)             throws IllegalArgumentException,                    IllegalAccessException  

Parameters: This method accepts a single parameter obj which is the object to extract the int value from.

Return value: This method returns the value of field converted to type int.

Exception: This method throws following Exception:

  1. IllegalAccessException: if Field object is enforcing Java language access control and the underlying field is inaccessible.
  2. IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field or if the field value cannot be converted to the type int by a widening conversion.
  3. NullPointerException: if the specified object is null and the field is an instance field.
  4. ExceptionInInitializerError: if the initialization provoked by this method fails.

Below programs illustrate getInt() method:
Program 1:




// Java program to demonstrate getInt() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Create the Employee class object
        Employee Employee = new Employee();
  
        // Get the Salary field object
        Field field
            = Employee.class.getField("Salary");
  
        // Apply getInt Method on Employee Object
        // to get the value of Salary field
        int value = field.getInt(Employee);
  
        // print result
        System.out.println("Value of int Field"
                           + " Salary is " + value);
    }
}
  
// sample Employee class
class Employee {
  
    // static int values
    public static int Salary = 34113;
    public static String name = "Aman";
  
    public static int getSalary()
    {
        return Salary;
    }
  
    public static void setSalary(int Salary)
    {
        Employee.Salary = Salary;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        Employee.name = name;
    }
}
 
 
Output:
  Value of int Field Salary is 34113  

Program 2:




// Java program to demonstrate getInt() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Create the IntNumbers class object
        IntNumbers Int = new IntNumbers();
  
        // Get the value field object
        Field field
            = IntNumbers.class.getField("value");
  
        // Apply getInt Method on field Object
        // to get the value of value field
        int value = field.getInt(Int);
  
        // print result
        System.out.println("Value: " + value);
    }
  
    // IntNumbers class
    static class IntNumbers {
  
        // int field
        public static int value = 999994567;
  
        // getter and setter methods
        public static int getValue()
        {
            return value;
        }
  
        public static void setValue(int value)
        {
            IntNumbers.value = value;
        }
    }
}
 
 
Output:
  Value: 999994567  

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getInt-java.lang.Object-



Next Article
Field getType() method in Java with Examples

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Field
  • Java-Functions
  • java-lang-reflect-package
Practice Tags :
  • Java

Similar Reads

  • Field get() method in Java with Examples
    The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an
    3 min read
  • Field getType() method in Java with Examples
    The getType() method of java.lang.reflect.Field used to get the declared type of the field represented by this Field object.This method returns a Class object that identifies the declared type Syntax: public String getType() Parameters: This method accepts nothing. Return value: This method returns
    2 min read
  • Field getLong() method in Java with Examples
    The getLong() method of java.lang.reflect.Field used to get the value of long which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type long via a widening conversion. When a class contains a static or instance long field and
    3 min read
  • Field getShort() method in Java with Examples
    The getShort() method of java.lang.reflect.Field used to get the value of short which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type short via a widening conversion. When a class contains a static or instance short field
    3 min read
  • Field getAnnotation() method in Java With Examples
    The getAnnotation() method of java.lang.reflect.Field is used to return returns Field objects for the specified type if such an annotation is present, else null.This is important method to get annotation for Field object. Syntax: public <T extends Annotation> T getAnnotation(Class<T> ann
    2 min read
  • Field getGenericType() method in Java with Examples
    The getGenericType() method of java.lang.reflect.Field used to return a Type object representing the declared type of this Field object. The returned type object can be one of the implementations of Type's subinterfaces: GenericArrayType, ParameterizedType, WildcardType, TypeVariable, Class. If the
    2 min read
  • Field getAnnotatedType() method in Java With Examples
    The getAnnotatedType() method of java.lang.reflect.Field is used to return an annonatedType object that represents the use of a type to specify the declared type of the field. Every Field of the class is declared by some AnnotatedType.AnnotatedType represents the potentially annotated use of any typ
    2 min read
  • Field set() method in Java with Examples
    The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s
    4 min read
  • Field isSynthetic() method in Java with Examples
    The isSynthetic() method of java.lang.reflect.Field is used to check whether Field Object is a synthetic field or not. If the field is a synthetic field then the function returns true otherwise it will return false. Synthetic Construct: Synthetic Construct is Class, Fields, and Methods that are crea
    2 min read
  • FieldPosition getField() method in Java with Example
    The getField() method of java.text.FieldPosition class is used to retrieve the field identifier of this field position object.Syntax: public int getField() Parameter: This method does not accepts any argument as parameter.Return Value: This method returns the field identifier of this field position
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences