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

  • Home
  • SEARCH
  • 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 6077951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:46:43+00:00 2026-05-23T10:46:43+00:00

I have the following code within a program I’m making: 01 public class Clazz<T>

  • 0

I have the following code within a program I’m making:

01  public class Clazz<T>
02  {
03    T[] t;
04    
05    public Clazz<T> methodA(int... ints)
06    {
07      Clazz<Integer> ints2 = new Clazz<>();
08      int remInd[] = new int[t.length - ints2.t.length];
09      return this;
10    }
11  }

but when I run method methodA, I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
    at Clazz.methodA(Clazz.java:8)

Why would I get this error? Of course, the code I’ve shown is incomplete compared to the huge class in question (for instance, the array t won’t be empty when checking its length), but I believe I’ve shown everything that matters. Why won’t this run?

A note: I am making this with JDK 1.7, so that’s why line 7 compiles and works

Working Solution


I decided, for whatever reason, to implement the following solution, and it worked:

01  public class Clazz<T>
02  {
03    T[] t;
04    
05    public Clazz<T> methodA(int... ints)
06    {
07      Clazz<Integer> ints2 = new Clazz<>();
08      int remInd[] = new int[t.length - ints2.length()];
09      return this;
10    }
11    
12    public int length()
13    {
14      return t.length;
15    }
16  }

Though this is a solution, I would still like to know why it works.

  • 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-05-23T10:46:44+00:00Added an answer on May 23, 2026 at 10:46 am

    You are missing the code that initializes T, but I’m going to assume it looks something like this. I’ve added a few lines that don’t change any functionality but which will help demonstrate the error:

    public class Clazz<T> {
        T[] t = (T[]) new Object[5];
    
        public Clazz<T> methodA(int... ints) {
            Clazz<Integer> ints2 = new Clazz<Integer>();
            int l1 = t.length;
            int l2 = ints2.t.length;
            int remInd[] = new int[l1 - l2];
            return this;
        }
    
        public static void main(String...args) {
            Clazz<String> clazz = new Clazz<String>();
            clazz.methodA(54, 7);
        }
    }
    

    With this code I could reproduce the error. The problem here is in this code:

    int l2 = ints2.t.length
    

    Since the compiler knows the type parameter for ints2 and thus ints2.t, this can be thought of as the rough equivalent of this:

    Integer[] temp = ints2.t;
    int l2 = temp.length;
    

    It is in the implicit cast to Integer[] (whose class simple name is [Ljava.lang.Integer) that this fails, since t is an Object[] and not an Integer[], and one cannot be cast to the other.

    Working with generic arrays

    There are many complications from working with arrays declared over a generic type that are documented elsewhere. In short, I’ll say that if you need to have a “generic array” instead consider declaring and using it as an Object[] in every way, except that when you interact with a client of the class, you either accept or return only a T instead of an Object (for returning, via an unchecked cast). For example,

    Object[] t = new Object[5];
    
    public T getSomethingFromArray() {
        return (T)t[2];
    }
    
    public void setSomethingInArray(T something) {
        t[2] = something;
    }
    

    This is how ArrayList works, by the way. Have a look at its code on DocJar.

    Edit

    Generic arrays aside, I don’t think you understand the idea of the implicit cast. Here’s much shorter code that fails with essentially the same error:

    public class Clazz<T> {
        T t = (T) new Object();
    
        public static void main(String...args) {
            Clazz<String> clazz = new Clazz<String>();
            clazz.t.toString();
        }
    }
    
    Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
        at Clazz.main(Clazz.java:6)
        ...
    

    Even though there is no need to cast clazz.t to a String, it does so implicitly simply by referencing clazz.t. Here is the javap -c output for that compiled class:

    Compiled from "Clazz.java"
    public class Clazz extends java.lang.Object{
    java.lang.Object t;
    
    public Clazz();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   aload_0
       5:   new #2; //class java/lang/Object
       8:   dup
       9:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       12:  putfield    #3; //Field t:Ljava/lang/Object;
       15:  return
    
    public static void main(java.lang.String[]);
      Code:
       0:   new #4; //class Clazz
       3:   dup
       4:   invokespecial   #5; //Method "<init>":()V
       7:   astore_1
       8:   aload_1
       9:   getfield    #3; //Field t:Ljava/lang/Object;
    //BELOW is the line that will fail
       12:  checkcast   #6; //class java/lang/String
       15:  invokevirtual   #7; //Method java/lang/String.toString:()Ljava/lang/String;
       18:  pop
       19:  return
    
    }
    

    In the case of your original code, here is the javap -c output of methodA():

    public Clazz methodA(int[]);
      Code:
       0:   new #5; //class Clazz
       3:   dup
       4:   invokespecial   #6; //Method "<init>":()V
       7:   astore_2
       8:   aload_0
       9:   getfield    #4; //Field t:[Ljava/lang/Object;
       12:  arraylength
       13:  aload_2
       14:  getfield    #4; //Field t:[Ljava/lang/Object;
    //BELOW is the line that will fail
       17:  checkcast   #7; //class "[Ljava/lang/Integer;"
       20:  arraylength
       21:  isub
       22:  newarray int
       24:  astore_3
       25:  aload_0
       26:  areturn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code within a stored procedure. WHERE WPP.ACCEPTED = 1 AND
In the following code, I have verified that connecting to a URL from within
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following code class User attr_accessor :name end u = User.new u.name =
Let's say I have the following within my source code, and I want to
I'm writing some code where I defined the following base class. class Chorus{ public:
I have a program that has the following code: foreach (string section in DataAccessLayer.AcceptedSections)
ok, i have the following class in my code behind file. it's a very
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in

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.