Results 1 to 4 of 4
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default FInal field cannot be assigned

    I have following code which raises exception:
    Java Code:
    public class Test {
        static final int STACK_EMPTY = -1;
        static int STACK_FULL = 10;
        public static void main(String[] args) {
        STACK_FULL = 12;		
        STACK_EMPTY = 2;
        }
    }
    Exception:
    Java Code:
    The final field Test.STACK_EMPTY cannot be assigned
    If you cant reassign value to a final attribute, then what is the use of it?

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Its used for constants (note that constants, by style, should be all capital letters, separated by underscores). For something you never want to change by accident in your code:
    Java Code:
    public final MAX_NUMBER_USERS = 10;
    final will just ensure that that number is never changed.

  3. #3
    gulapala is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default

    Please know the difference when the final keyword is used

    1. with variables.
    2. with methods.
    3. with classes.

    It would help you.
    Thanks & Regards, G.Rajasekhar

  4. #4
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks all of you.
    Its clear in my mind now. I feel I am improving now :)

Similar Threads

  1. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  2. Reading a Field in a Binary File
    By janakiram.attuluri in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 10:47 AM
  3. Help with define an integer field
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:32 AM
  4. problem with date field
    By Ed in forum JDBC
    Replies: 2
    Last Post: 06-12-2007, 04:27 PM
  5. 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
  •