Solved

need simple explanition for a simple code

Posted on 2002-04-28
9
173 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 159
stackato and cloud 4 85
Spring Framework HTTPSession management 1 33
ForLoop Example 3 47
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now