Problem defining a constant in java

In summary, the conversation discusses a syntax error on line 1 when defining a constant with a semicolon, and possible solutions involving the definition of "escenario" as a class and the use of square brackets for array access.
  • #1
stonecoldgen
109
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
  • #2
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).
 
  • #3
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
 
  • #4
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?
 
  • #5
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];
 

1. What is a constant in Java?

A constant in Java is a variable that holds a fixed value and cannot be changed during the execution of a program. It is declared using the keyword "final" and is often used to store values that are not expected to change, such as mathematical constants or program settings.

2. How do you define a constant in Java?

To define a constant in Java, you can use the "final" keyword before the variable declaration. For example, "final int MAX_VALUE = 10;" will declare a constant named MAX_VALUE with a value of 10. This variable can then be used throughout the program without being able to be reassigned.

3. Can a constant be changed in Java?

No, a constant cannot be changed in Java. Once a constant is declared using the "final" keyword, its value cannot be modified. Attempts to reassign a value to a constant will result in an error during compilation.

4. Why should constants be used in Java?

Constants should be used in Java because they help improve code readability and maintainability. By declaring a value as a constant, it is clear to other developers that the value is not meant to be changed. This can also prevent accidental changes to important values in the program.

5. Is there a naming convention for constants in Java?

Yes, constants should be named using all capital letters and words separated by underscores. This naming convention makes it clear that the variable is a constant and helps distinguish it from other variables in the code.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
2
Views
628
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top