I have a compilation problem in a Java program:
class FigureEditor {
int[] a; ----- Syntax error on token ";", , expected
a = new int[5];
}
What am I doing wrong?
Before creating an array of objects, we must create an instance of the class by using the new keyword. We can use any of the following statements to create an array of objects. Syntax: ClassName obj[]=new ClassName[array_length]; //declare and instantiate an array of objects.
Yes, since objects are also considered as datatypes (reference) in Java, you can create an array of the type of a particular class and, populate it with instances of that class.
Arrays can be declared as the members of a class. The arrays can be declared as private, public or protected members of the class. To understand the concept of arrays as members of a class, consider this example.
An example of class with an array member with value initialisation: struct foo { int member[10] = {}; };
You can't have "floating" statements in the class body.
Either initialize it directly:
int[] a = new int[5];
Or use an initializer block:
int[] a;
{
a = new int[5];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With