?
Solved

need simple explanition for a simple code

Posted on 2002-04-28
9
Medium Priority
?
177 Views
Last Modified: 2010-03-31
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"
  };
0
Comment
Question by:star4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 200 total points
ID: 6975303
star4,

What you've listed is not a function but an array. You can access the array like this: COLORS[2]

The "final" keyword for variables means that the value will never change. It lets Java optimize the code better. Your statement will work without "final", but then it will be a normal array which you can modify in your code. If you're familiar with C, 'final' is similar to 'const'.

Yuval
0
 
LVL 1

Expert Comment

by:XAN
ID: 6987548
you need "final" if you define this variable in internal class
0
 
LVL 7

Expert Comment

by:yoren
ID: 6987565
XAN, do you mean an inner class? Inner classes cannot have static variables at all. There is no requirement for "final" though.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:XAN
ID: 7007224
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 ;)
0
 
LVL 1

Expert Comment

by:XAN
ID: 7007225
But, youren, you are absolutely right this is NOT FUNCTION :)
0
 
LVL 7

Expert Comment

by:yoren
ID: 7007243
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.

0
 
LVL 1

Expert Comment

by:XAN
ID: 7007250
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
0
 
LVL 5

Expert Comment

by:vemul
ID: 7898811
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7959836
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question