Friday, December 19, 2014

Java elements

0. Static means that can be access in an static way (that not need to instanced the class like new ExampleClass)
You can treat static as a global variable which has scope. It basically means if you change it for one object it will be changed for all just like a global variable(limited by scope).

1. public static final:
final indicates a variable who's value can't be modified after it is declared.

Use public final static String when you want to create a String that belongs to the class (no instance necessary to use it), and that won't change, for instance when you want to define a String constant that will be available to all instances of the class, and to other objects using the class, depending on the access modifier:

public final static String MY_CONSTANT = "SomeValue";

It isn't required to use final, but it keeps a constant from being changed inadvertently during program execution, and serves as an indicator that the variable is a constant.

Even if the constant will only be used - read - in the current class and/or in only one place, it's clearer to declare all constants as final, and still good practice, since during the lifetime of the code the constant may end up being used in more than one place.

Furthermore using final may allow the implementation to perform some optimization, e.g. by inlining an actual value where the constant is used.
http://stackoverflow.com/questions/11677670/when-exactly-are-we-supposed-to-use-public-static-final-string

2. The java.lang.Class.getSimpleName() returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous.
http://www.tutorialspoint.com/java/lang/class_getsimplename.htm


No comments:

Post a Comment