Problem defining a constant in java

  • Context: Java 
  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Constant Java
Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue related to defining a constant and initializing an array of objects. Participants explore syntax errors encountered when declaring a constant and creating an array of a class type.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports a syntax error when defining a constant and initializing an array, questioning the cause of the error.
  • Another participant asks if the class "escenario" has been defined and suggests following Java naming conventions.
  • A participant confirms the class was defined but notes that the name was incorrectly cased, leading to further issues.
  • One participant questions the use of square brackets in the array initialization, suggesting a possible misunderstanding of array versus object instantiation.
  • Another participant clarifies that if an array of "Escenario" objects is intended, the declaration should specify the array type correctly.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correct class naming and syntax but offer differing views on the specific cause of the initial syntax error and the correct approach to array initialization.

Contextual Notes

There are unresolved issues regarding the exact nature of the syntax error and the correct usage of array versus object instantiation in Java.

stonecoldgen
Messages
108
Reaction score
0
I have:

Code:
public final static int NUMERO_ESCENARIOS=4;

escenarios = new escenario[NUMERO_ESCENARIOS];

having an error in the first line that says: Syntax error on token ";", , expected


but when I have:
Code:
public final static int NUMERO_ESCENARIOS=4 //semi colon deleted

escenarios = new escenario[NUMERO_ESCENARIOS];

I get no error in the definition of the constant but I obviously get a syntax error in the array definition

and when I have:

Code:
public final static int NUMERO_ESCENARIOS=4;

//array definition errased

the constant definition is fine





so what the hell is happening? thanks
 
Technology news on Phys.org
Have you defined "escenario" as a class? (by the way, it is considered good Java practice to spell you classes with an initial upper case letter).
 
Filip Larsen said:
Have you defined "escenario" as a class? (by the way, it is considered good Java practice to spell you classes with an initial upper case letter).

yes, and now that you mention it, I found out that escenario was actually Escenario (so I changed everything in my code to have that uppercase)

however, I still have the initial problem
 
stonecoldgen said:
I have:

Code:
public final static int NUMERO_ESCENARIOS=4;

escenarios = new escenario[NUMERO_ESCENARIOS];

having an error in the first line that says: Syntax error on token ";", , expected

If Escenario is a class, why are you using square brackets (which are used to access an array or List element) to pass NUMERO_ESCENARIOS to the constructor method? Shouldn't you have something like

Code:
public final static int NUMERO_ESCENARIOS=4;

Escenario escenarios = new Escenario(NUMERO_ESCENARIOS);

instead?
 
Or, if you are trying to create an array of Escenario objects, then you would first need to define escenarios to be an Array variable of type Escenario:

Code:
public final static int NUMERO_ESCENARIOS=4;

Escenario[] escenarios = new Escenario[NUMERO_ESCENARIOS];
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K