Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:28:26+00:00 2026-06-05T22:28:26+00:00

class Test { public static void main(String[] args) throws Exception { Test t =

  • 0
class Test {
    public static void main(String[] args) throws Exception {
        Test t = new Test();
        System.out.println(t.field);
        System.out.println(t.getClass().getField("field").get(t));

        int[] ar = new int[23];
        System.out.println(ar.length);
        System.out.println(ar.getClass().getField("length").get(ar));

    }
    public int field = 10;
};

When I run the above code, I get the following result on the command line —

10
10
23
Exception in thread "main" java.lang.NoSuchFieldException: length
    at java.lang.Class.getField(Class.java:1520)
    at Test.main(Test.java:9)

How come I am unable to access the field “length” in the array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:28:27+00:00Added an answer on June 5, 2026 at 10:28 pm

    I think this might be a bug in the JVM implementation. Here is my reasoning:

    1. According to the documentation for Class.getField, getField should, as part (1) of its search algorithm, find length if it was declared as a public field: “If C declares a public field with the name specified, that is the field to be reflected.”

    2. According to the Java Language Specification, every array has length declared as “The public final field length, which contains the number of components of the array.”

    3. Since this field is declared as having name length, getField should either throw a SecurityException as documented, or should return the Field object.

    Now interestingly, the Class.getFields method explicitly mentions that “The implicit length field for array class is not reflected by this method. User code should use the methods of class Array to manipulate arrays.” This does not seem to parallel getField, so this could either be a misreading on my part or just bad documentation.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test { public static void main(String[] args) throws Exception { String s1
import javax.swing.*; public class test { public static void main(String[] args) throws Exception {
Here is the code: class Test { public static void main (String[] args) throws
main method: public static void main(String[] args) throws Exception { if (args.length != EXPECTED_NUMBER_OF_ARGUMENTS)
Take a look at this code: public class Test { public static void main(String...
I have following code public class TEST { public static void main(String arg[]){ try
using System; using System.Math; class test { public static void Main() { Console.Write(Enter any
class test { static int arr[]={1,6,3,4,5,8,11}; static int s=0,temp=0,e=0; public static void main(String []
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Here is my code: public class Test { static { main(null); } public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.