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 7875913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:04:45+00:00 2026-06-03T03:04:45+00:00

I have a Java JUnit 4 test and a generic class which creates a

  • 0

I have a Java JUnit 4 test and a generic class which creates a generic array. When I create a method to return this generic array I get an error message upon returning it. It works if I return an ArrayList (where T is Byte by class instantiation) to an ArrayList.

Why is it that the array appears to be instantiated and usable (tested on more complex classes) yet I can’t return a reference to that array?

Error:

java.lang.ClassCastException: [Lcom.waikato.assignment.Object; cannot be cast to [Ljava.lang.Byte;
at com.waikato.testing.TestJava.test(TestJava.java:20)

Class:

public class TestClass<T> {
    T[] array;

    @SuppressWarnings("unchecked")
    public TestClass() {
        array = (T[]) new Object[32];
    }

    public T[] getArray() {
        return array;
    }

    public boolean doThing() {
        T[] t = array;

        return t == array && t != null;
    }
}

Test:

public class TestJava {

    @Test
    public void test() {
        TestClass<Byte> t = new TestClass<Byte>();
        Byte[] b = t.getArray(); // Crash caused

        assertTrue(t.doThing()); // Works when above line is removed.
        assertTrue(b.length != 0);
    }

}
  • 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-03T03:04:46+00:00Added an answer on June 3, 2026 at 3:04 am

    A “generic” array can be constructed via reflection:

    T[] array = (T[]) Array.newInstance(Byte.class, 32)
    

    Replace Byte.class by a reference to the desired class. In other words:

    public class TestClass<T> {
        T[] array;
    
        @SuppressWarnings("unchecked")
        public TestClass(Class<T> type) {
            array = (T[]) Array.newInstance(type, 32);
        }
    
        public T[] getArray() {
            return array;
        }
    
        public boolean doThing() {
            T[] t = array;
    
            return t == array && t != null;
        }
    }
    

    You can verify this as such:

    public static void main(String[] args) {
        TestClass<Byte> test = new TestClass<Byte>(Byte.class);
        // No ClassCastException here
        Byte[] array = test.getArray();
        System.out.println(Arrays.asList(array));
    }
    

    Due to type erasure, you cannot solve this problem without resorting to a Class<?> object as a constructor argument.

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

Sidebar

Related Questions

I'm trying to create an EJB factory class, which works like this: You have
I am getting this error in java in my junit test code. I looked
I am fairly new to Java. I have constructed a single JUnit test class
I have a Java Project in which I am writing a simple JUNIT test
I have a project in Java with JUnit tests in Scala. Each test class
Class under test MyClass.java JUnit test case name alternatives: TestMyClass.java MyClassTest.java http://moreunit.sourceforge.net seems to
When i run my spring junit test i got this error message : Caused
Here is my JUnit test class: package com.bynarystudio.tests.storefront; import java.util.List; import org.junit.Assert; import org.junit.Test;
I have two jUnit test classes, one for testing my ItemService class, the other
I have a junit test method that takes a CommonsMultipartFile object as a parameter.

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.