10 points about Static in Java
http://java67.blogspot.com/2012/11/what-is-static-class-variable-method.html
1) You can not access non static member inside static context e.g. static method or static block.
2) Unlike local variables, Static variables and methods are not thread-safe in Java
4) Another important point about static method is that, you can not override static method in Java. If you declare same method in sub class i.e. static method with same name and method signature
it will only hide super class method, instead of overriding it. This is also known as method hiding in Java. What this means is, if you call a static method, which is declared in both super class and sub class, method is always resolved at compile time by using Type of reference variable. Unlike case of method overriding they will not resolved during runtime.
5) You can also make a class static in Java, except top level classes.
6) static keyword can also be used to declare static block which is executed during class loading.
This is known as static initializer block in Java. If you don't declare a static initializer block by yourself then Java combines all static fields into one block and execute them during class loading. Though static block can not throw checked exception, they can still throw unchecked exception, which if occurred may result in ExceptionInitializerError. Actually any runtime exception thrown during instantiation and initialization of static fields, will be wrapped by Java runtime into this error. This is also one of the most common reason of NoClassDefFoundError in Java, because the class in question was not present in memory when its client needed them.
7) One more thing to know about static methods is that they are bonded during compile time using static binding.
8) One of the important property of static field is initialization. Static fields or variables are initialized when class is loaded into memory. They are initialized from top to bottom in the order they are declared in Java source file.
9) During Serialization, like transient variables, static variables are also not serialized. It means, if you store any data in static filed then after de-serialization, new object will contain its default value e.g.
10) Another feature related to static keyword is called static import.
Why String Class is made Immutable or Final
http://java67.blogspot.com/2014/01/why-string-class-has-made-immutable-or-final-java.html
1) String Pool
store String literals in String pool. Goal was to reduce temporary String object by sharing them and in order to share, they must have to be from Immutable class.
4) Multithreading Benefits
5) Optimization and Performance
String cache its hashcode. It even calculate hashcode lazily and once created, just cache it. In simple world, when you first call hashCode() method of any String object, it calculate hash code and all subsequent call to hashCode() returns already calculated, cached value.
2) Security
If String was not immutable, a user might have granted to access a particular file in system, but after authentication he can change the PATH to something else, this could cause serious security issues. Similarly, while connecting to database or any other machine in network, mutating String value can pose security threats. Mutable strings could also cause security problem in Reflection as well, as the parameters are strings.
3) Use of String in Class Loading Mechanism
As String been not Immutable, an attacker can take advantage of this fact and a request to load standard Java classes e.g. java.io.Reader can be changed to malicious class.
5) Hashmap keys
It's one of the most popular object to be used as key in hash based collections e.g. HashMap and Hashtable. Though immutability is not an absolute requirement for HashMap keys, its much more safe to use Immutable object as key than mutable ones, because if state of mutable object is changed during its stay inside HashMap, it would be impossible to retrieve it back, given it's equals() and hashCode() method depends upon the changed attribute.
Disadvantages
Since String is immutable, it generates lots of temporary use and throw object, which creates pressure for Garbage collector.
new String() will not pick object from String pool.
String pool is located in PermGen Space of Java Heap, which is very limited as compared to Java Heap. Having too many String literals will quickly fill this space, resulting in java.lang.OutOfMemoryError: PermGen Space.
from Java 7 onwards, they have moved String pool to normal heap space, which is much much larger than PermGen space.
http://java67.blogspot.com/2012/11/what-is-static-class-variable-method.html
1) You can not access non static member inside static context e.g. static method or static block.
2) Unlike local variables, Static variables and methods are not thread-safe in Java
4) Another important point about static method is that, you can not override static method in Java. If you declare same method in sub class i.e. static method with same name and method signature
it will only hide super class method, instead of overriding it. This is also known as method hiding in Java. What this means is, if you call a static method, which is declared in both super class and sub class, method is always resolved at compile time by using Type of reference variable. Unlike case of method overriding they will not resolved during runtime.
5) You can also make a class static in Java, except top level classes.
6) static keyword can also be used to declare static block which is executed during class loading.
This is known as static initializer block in Java. If you don't declare a static initializer block by yourself then Java combines all static fields into one block and execute them during class loading. Though static block can not throw checked exception, they can still throw unchecked exception, which if occurred may result in ExceptionInitializerError. Actually any runtime exception thrown during instantiation and initialization of static fields, will be wrapped by Java runtime into this error. This is also one of the most common reason of NoClassDefFoundError in Java, because the class in question was not present in memory when its client needed them.
7) One more thing to know about static methods is that they are bonded during compile time using static binding.
8) One of the important property of static field is initialization. Static fields or variables are initialized when class is loaded into memory. They are initialized from top to bottom in the order they are declared in Java source file.
9) During Serialization, like transient variables, static variables are also not serialized. It means, if you store any data in static filed then after de-serialization, new object will contain its default value e.g.
10) Another feature related to static keyword is called static import.
Why String Class is made Immutable or Final
http://java67.blogspot.com/2014/01/why-string-class-has-made-immutable-or-final-java.html
1) String Pool
store String literals in String pool. Goal was to reduce temporary String object by sharing them and in order to share, they must have to be from Immutable class.
4) Multithreading Benefits
5) Optimization and Performance
String cache its hashcode. It even calculate hashcode lazily and once created, just cache it. In simple world, when you first call hashCode() method of any String object, it calculate hash code and all subsequent call to hashCode() returns already calculated, cached value.
2) Security
If String was not immutable, a user might have granted to access a particular file in system, but after authentication he can change the PATH to something else, this could cause serious security issues. Similarly, while connecting to database or any other machine in network, mutating String value can pose security threats. Mutable strings could also cause security problem in Reflection as well, as the parameters are strings.
3) Use of String in Class Loading Mechanism
As String been not Immutable, an attacker can take advantage of this fact and a request to load standard Java classes e.g. java.io.Reader can be changed to malicious class.
5) Hashmap keys
It's one of the most popular object to be used as key in hash based collections e.g. HashMap and Hashtable. Though immutability is not an absolute requirement for HashMap keys, its much more safe to use Immutable object as key than mutable ones, because if state of mutable object is changed during its stay inside HashMap, it would be impossible to retrieve it back, given it's equals() and hashCode() method depends upon the changed attribute.
Disadvantages
Since String is immutable, it generates lots of temporary use and throw object, which creates pressure for Garbage collector.
new String() will not pick object from String pool.
String pool is located in PermGen Space of Java Heap, which is very limited as compared to Java Heap. Having too many String literals will quickly fill this space, resulting in java.lang.OutOfMemoryError: PermGen Space.
from Java 7 onwards, they have moved String pool to normal heap space, which is much much larger than PermGen space.