Link to home
Start Free TrialLog in
Avatar of star4
star4

asked on

need simple explanition for a simple code

i have this function to produce different colors ,
why i need the word final here , what is its function ?
does the function work with out it ?

public static final String[] COLORS={
       "#EEE8AA","#FFEFD5","#FFDAB9","#FFFFFF"
  };
ASKER CERTIFIED SOLUTION
Avatar of yoren
yoren

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XAN
XAN

you need "final" if you define this variable in internal class
XAN, do you mean an inner class? Inner classes cannot have static variables at all. There is no requirement for "final" though.
Hi, youren.
What about this:

public class Task {
  class subtask {
    static final String st_fin = "THIS IS STATIC!";
    static String st_non_fin = "THIS IS STATIC NOT FINAL";
    public subtask(){}
  }
  public Task() {}
}


Please compile this code and tell me about your experience with it ;)
But, youren, you are absolutely right this is NOT FUNCTION :)
Your code demonstrates that you cannot have non-final static variables in inner classes. Technically, inner classes can have static variables if declared final, but I don't really consider those variables. They're really just constants.

CONSTATNS in java is just STATIC FINAL VARIABLES (by definition)

So, let return to our question. The code

public static final String[] COLORS={
      "#EEE8AA","#FFEFD5","#FFDAB9","#FFFFFF"
 };

couldn't work without word 'final' if it is definition belongs to inner class.

Tell me, please, you are agree, and that
  "Inner classes can have static variables at all" :)

Cheers ;)
me
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to yoren

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
Comment from expert accepted as answer

Computer101
E-E Admin