Solved

need simple explanition for a simple code

Posted on 2002-04-28
9
176 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 50 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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