Results 1 to 4 of 4
  1. #1
    techie.it19 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default static final field

    class SwitchDemo {
    public static void main(String[] args) {

    int month = 8;
    final int Eight = 2;
    switch (month) {
    case 1: System.out.println("January"); break;
    case Eight: System.out.println("February"); break;
    default: System.out.println("Invalid month.");break;
    }
    }
    }
    In the above code why can't we declare variable Eight as Static final. It is not allowed. It says only final is valid type. Please advise.
    thanks in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Java is case sensitive. Not Static. static.

    db

    edit No, I see that was probably just a typo you made while posting.

    static variables can only be declared at the class level. A method-local variable can't be static.

  3. #3
    techie.it19 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    thanks daryll

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, if you want to define a static variable, you should do it in class level. Reason is static variables are declared at the time of class loading, not in compile time or runtime.

Similar Threads

  1. Final,abstract and static
    By $hr!k@nt in forum Advanced Java
    Replies: 5
    Last Post: 01-09-2008, 05:23 AM
  2. FInal field cannot be assigned
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 02:26 PM
  3. Final field question
    By derrickD in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2007, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •