Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem defining a constant in java

  1. Oct 1, 2012 #1
    I have:

    Code (Text):
    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 (Text):
    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 (Text):
    public final static int NUMERO_ESCENARIOS=4;

    //array definition errased
    the constant definition is fine





    so what the hell is happening? thanks
     
  2. jcsd
  3. Oct 1, 2012 #2

    Filip Larsen

    User Avatar
    Gold Member

    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).
     
  4. Oct 1, 2012 #3
    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
     
  5. Oct 1, 2012 #4

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    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 (Text):
    public final static int NUMERO_ESCENARIOS=4;

    Escenario escenarios = new Escenario(NUMERO_ESCENARIOS);
    instead?
     
  6. Oct 1, 2012 #5

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    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 (Text):
    public final static int NUMERO_ESCENARIOS=4;

    Escenario[] escenarios = new Escenario[NUMERO_ESCENARIOS];
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Problem defining a constant in java
  1. Simple Java Problem (Replies: 2)

  2. Java compile problems (Replies: 2)

Loading...