Uninitialized Array Assignment Syntax
Java allows for concise initialization of arrays within declarations:
AClass[] array = {object1, object2};
However, attempting to assign an uninitialized array with curly braces results in a compiler error:
AClass[] array; ... array = {object1, object2}; // Error
Reason for Restriction
The specific reason for this restriction is unclear. It may be due to grammatical complexities or the desire to maintain consistency in Java's syntax.
Workaround
Although not as concise, you can initialize an uninitialized array with the new operator and then assign elements explicitly:
AClass[] array; ... array = new AClass[2]; ... array[0] = object1; array[1] = object2;
Simplified Example
Using this workaround in the provided code snippet simplifies the array initialization logic:
public void selectedPointsToMove(cpVect coord) { if (tab == null) { if (arePointsClose(coord, point1, 10)) tab = new cpVect[]{point1}; else if (arePointsClose(point2, coord, 10)) tab = new cpVect[]{point2}; else tab = new cpVect[]{point1, point2}; } }
The above is the detailed content of Why Does Java Prevent Uninitialized Array Assignment with Curly Braces?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools