Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Eclipse for Java syntax checker error for generic arrays
Eclipse for Java syntax checker error for generic arrays [message #1862674] Wed, 20 December 2023 03:57 Go to next message
Eclipse UserFriend
The following line does not compile by hand manually with javac (JDK21), and does not go past Eclipse syntax checker (JDK-17 compat), which claims its a generic array, which is clearly not, very specifically typed expression.

MyClass<Long>[] longsort = new MyClass<Long>[5];

[Updated on: Wed, 20 December 2023 04:01] by Moderator

Re: Eclipse for Java syntax checker error for generic arrays [message #1864892 is a reply to message #1862674] Fri, 19 April 2024 00:09 Go to previous messageGo to next message
Eclipse UserFriend
In Java, you cannot directly create arrays of a generic type. This is because generic types are erased at runtime, and the JVM needs to know the specific type that the array will hold to ensure type safety. This code suppresses the unchecked warning because we are manually ensuring that the array will only contain MyClass<Long> instances. It's important to be cautious with this approach to avoid introducing potential ClassCastExceptions at runtime.

MyClass<Long>[] longsort = new MyClass<Long>[5];
@SuppressWarnings("unchecked")
MyClass<Long>[] longsort = (MyClass<Long>[])new MyClass[5];
ArrayList<MyClass<Long>> longsort = new ArrayList<>(5);

Re: Eclipse for Java syntax checker error for generic arrays [message #1864956 is a reply to message #1864892] Mon, 22 April 2024 01:54 Go to previous message
Eclipse UserFriend
Woah, that type mismatch is definitely head-scratching! You're right, it should compile. Here's what might be going on:

JDK Version: While you mentioned JDK21 for javac, sometimes Eclipse can have a different version configured. Double-check both versions to ensure compatibility.
Generic Array Syntax: There might be a quirk with how Eclipse handles generic array syntax. Try the following alternative:
Java
MyClass<Long> longsort[] = new MyClass[5];

This rearranges the brackets to explicitly declare the array type first.

If neither of those suggestions work, try creating a minimal reproducible example and posting it on a Java forum like Stack Overflow. Someone might have encountered the same issue with your specific Eclipse setup.
Previous Topic:keep show settings classpath containers 36%
Next Topic:Selectively redirect only stdout to a file
Goto Forum:
  


Current Time: Mon Jun 16 13:32:40 EDT 2025

Powered by FUDForum. Page generated in 0.06354 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top