Results 1 to 6 of 6
  1. #1
    emilioJazz is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Red face Final variables shouldn't also be static?

    Hi all! There's a theoric question wandering around in my mind.....
    Shouldn't be FINAL variables declared in classes also automatically be considered as STATIC?
    Because if I can't change it, it would be the same in all instances of the class...that is the definition of a STATIC variable. :rolleyes:
    Anyone has ideas?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So you're saying that this code shouldn't work, that two objects of the same class can't have different final class variables?

    Java Code:
    public class TestFinal {
       private final int myFinalVar;
       
       public TestFinal(int aVariable) {
          myFinalVar = aVariable;
       }
       
       public int getMyFinalVar() {
          return myFinalVar;
       }
       
       public static void main(String[] args) {
          TestFinal testFinal1 = new TestFinal(5);
          TestFinal testFinal2 = new TestFinal(3);
          
          System.out.println("testFinal1's var is " + testFinal1.getMyFinalVar());
          System.out.println("testFinal2's var is " + testFinal2.getMyFinalVar());
       }
    }

    But work it does.

  3. #3
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    but if i understand correctly from theory isn't the whole purpose of final is to ensure that value doesn't change or get changed in code some where?

    i am not giving an answer but rather seeking an elaboration.

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by f1gh View Post
    but if i understand correctly from theory isn't the whole purpose of final is to ensure that value doesn't change or get changed in code some where?
    Yes thats rite but if you notice Fubarable is calling two different objects of the class TestFinal. The final variable value cant be changed for the same instance.

    Lets keep the ball rolling ... sure to gain somthin from this :)


    warm regards
    Vinod M
    _______________________________________________
    give me beans .........

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    Quote Originally Posted by f1gh View Post
    but if i understand correctly from theory isn't the whole purpose of final is to ensure that value doesn't change or get changed in code some where?

    i am not giving an answer but rather seeking an elaboration.
    It can be (as shown by Fubarables code) assigned once. In the case of a class attribute that once has to be in the constructor (or at the declaration).

    This shows that final does not imply the same value across all instances.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Note that you can also declare local final variables, either as a parameter qualifier or in the body of the method, that the value can't change during the execution of the method.

    I.E. the final applies to the scope in which it is declared. static final is a class constant, final as an instance variable is an instance constant, and local is, of course, a local constant (which also, kind of, applies to method declarations, a final method declaration cannot be overridden in a subclass because the superclass has declared its version as final so it can't be changed).

Similar Threads

  1. static final int DONE = (1 << 9) - 1; ???
    By alacn in forum New To Java
    Replies: 2
    Last Post: 07-26-2010, 06:31 PM
  2. final variables in abstract classes
    By parulmahajan in forum New To Java
    Replies: 4
    Last Post: 06-11-2010, 10:54 AM
  3. Replies: 6
    Last Post: 02-15-2010, 05:31 AM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  5. Final,abstract and static
    By $hr!k@nt in forum Advanced Java
    Replies: 5
    Last Post: 01-09-2008, 06:23 AM

Posting Permissions

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