Stupid shortcut in Java for constants: final int flash=4;

K.I.L.E.R

Retarded moron
Veteran
Is it worth it? I read that there are no advantages in doing this but I like to do it so 'flash' cannot be changed.

Should I not get into the practice of doing it?
 
It's much better than having the numbers hard-coded in multiple spots at the code. This way if you have 5 functions which check a value, you only have to change it in one spot where you declare the constant instead of in 5 seperate spots. It also allows for the code to be more readable. It's a maintenance and sanity saver.
 
I've never used it, but isn't there supposed to be some enum class or something for this reason?
 
A sort of enum support is in the works for Java 1.5 ... Early Access Release is supposed to be during the winter quarter.
 
Thing about JAVA that I like is that it frequently gets updated. :D

My question should have been more clear, I apologise BRiT and to all the others.

Is there any performance hit in using the keyword "final"?
If so, how large of a performance hit?

Java is slow enough already, if in case there is a performance penalty I will make it my life goal to minimise it's use rather than use it every chance it would benefit me.

Thanks
 
There is no performance hit. I believe it'll be faster since the value will be immediate rather than requiring a fetch from a memory location.
 
Saem said:
There is no performance hit. I believe it'll be faster since the value will be immediate rather than requiring a fetch from a memory location.

Thanks.
Now I will go crazy with "final'. :D
 
Actually, the use of 'final' is encouraged. There is no performance hit with it. Quite the contrary, it brings performance enhancements. It hints to the compiler that the value will never change. It allows the compiler to perform certain optimizations that it otherwise would not be able to do.

For example, through the use of final on the parameters [one should never reassign parameters anyways], in most times the following can be compiled inline. Also, declaring a function as final means that one avoids the performance hit caused by the lookup of a virtual function table. It's fine if you never expect one to ever have to override the base functionality provided.
Code:
public final int log(final int level, final String msg)
{
    if (level <= logLevel) {
        logger.log(level, msg);
    }
}

Another trick is to use final on member variables without direct assignment, which means the values can only be set through the constructor. Thus, after the object is created, the value of the field can never change.
Code:
/**
 * The name of the Index.
 */
private final String name;

/**
 * Constructs an Index with the specified name.
 *
 * @param indexName The name of the index.
 */
public Index(final int indexName)
{
    name = indexName;
}
 
K.I.L.E.R said:
Thing about JAVA that I like is that it frequently gets updated. :D

That's something I don't like.
I think it's funny when people said C++ has too many "redundant" or "complex" features which Java don't need. Now Java is incorporating these features.
 
pcchen said:
K.I.L.E.R said:
Thing about JAVA that I like is that it frequently gets updated. :D

That's something I don't like.
I think it's funny when people said C++ has too many "redundant" or "complex" features which Java don't need. Now Java is incorporating these features.

Well you don't have to use them. I doubt many devs use ALL the features of Java anyway.

There are so many different ways of getting 1 job done it isn't funny.

The point I'm trying to make is that you can accomplish something anyway you like. Features are just there to either make the task quick, simple or quick and simple.
 
K.I.L.E.R said:
Well you don't have to use them. I doubt many devs use ALL the features of Java anyway.

There are so many different ways of getting 1 job done it isn't funny.

The point I'm trying to make is that you can accomplish something anyway you like. Features are just there to either make the task quick, simple or quick and simple.

You misunderstand me. What I think is, C++ is a multiple paradigm language. It is designed to have many ways to get a job done. On the other hand, Java is not. Java is designed to be an OOP language. At first, Java designers and users criticize C++ as a complex language, while Java is "clean." However, now it looks like Java is following C++'s step! If so, why bother designing a new language at first?
 
We can't argue for cross platform capabilities as C++ is just as portable as Java.
I either don't know enough about Java to argue with your conclusion or that's all there is to the Java language. :?

I believe Java is just a simpler version of C++ with extra bits added on to the end of it.

pcchen said:
K.I.L.E.R said:
Well you don't have to use them. I doubt many devs use ALL the features of Java anyway.

There are so many different ways of getting 1 job done it isn't funny.

The point I'm trying to make is that you can accomplish something anyway you like. Features are just there to either make the task quick, simple or quick and simple.

You misunderstand me. What I think is, C++ is a multiple paradigm language. It is designed to have many ways to get a job done. On the other hand, Java is not. Java is designed to be an OOP language. At first, Java designers and users criticize C++ as a complex language, while Java is "clean." However, now it looks like Java is following C++'s step! If so, why bother designing a new language at first?
:?
 
pcchen said:
You misunderstand me. What I think is, C++ is a multiple paradigm language. It is designed to have many ways to get a job done. On the other hand, Java is not. Java is designed to be an OOP language. At first, Java designers and users criticize C++ as a complex language, while Java is "clean." However, now it looks like Java is following C++'s step! If so, why bother designing a new language at first?

It is? Could you show me where?
 
Saem said:
I've never used it, but isn't there supposed to be some enum class or something for this reason?

Yes, there are in fact two: Iterator and Enumeration (Although the last one is deprecated IIRC.)
But they aren't as elegant as enums.
 
Snyder said:
It is? Could you show me where?

I don't follow Java's development closely, but it seems Java is getting some features C++ has, and people are proposing other features into Java, such as generic types. I don't know, but it looks like Java is getting more complex. Perhaps a useful real world language has to be that complex, after all.
 
pcchen said:
Snyder said:
It is? Could you show me where?

I don't follow Java's development closely, but it seems Java is getting some features C++ has, and people are proposing other features into Java, such as generic types. I don't know, but it looks like Java is getting more complex. Perhaps a useful real world language has to be that complex, after all.

Generics and enums have been on the most-wanted list of java developers for some time (including me - at least concerning generics, I don't miss enums much.). Including them does make the language more complex, but it doesn't break principal paradigms the language is based upon, such as for example the pointer model/gc/managed code, strict OOP or static typing.
 
Back
Top