Solved

need simple explanition for a simple code

Posted on 2002-04-28
9
175 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 38
servlet web applications   metadata-complete="true" or false 3 201
swing controls 2 23
JDBC performance 8 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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