Results 1 to 2 of 2
  1. #1
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default Final field question

    Why am I allowed to overwrite the final field PRIZE in a subclass? I expected a compile error but got nothing.
    Java Code:
    class Final {
        public static final String PRIZE = "$64,000";
    }
    
    public class TestFinal extends Final {
        public static final String PRIZE = "2 cents";
    
        public static void main(String[] args) {
          System.out.println(TestFinal.PRIZE);
        }
    }

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    The final modifier is different for methods than fields. On a method final means it may not be overridden or hidden. On a field, final means the field may not be assigned more than once. If you really want to make sure it keeps its original value then make a final method that returns the field.
    Java Code:
    public static final String getPrize(){
      return PRIZE;
    }

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 04:19 AM
  3. FInal field cannot be assigned
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 03:26 PM
  4. Help with define an integer field
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:32 AM
  5. problem with date field
    By Ed in forum JDBC
    Replies: 2
    Last Post: 06-12-2007, 05:27 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
  •