static
- static keyword makes any class member independent of the object.
- static can be applied on block,methods and global variables and also imports
- The member belongs to the class, not to objects of that class.
- You can actually import a static data member into another class and change its value as well
package packageOne; public class ClassOne { static public int i; static public int j; static public void hello() { System.out.println("Hello World!"); } }
package packageTwo; import static packageOne.ClassOne.*; public class ClassTwo { public ClassTwo() { i = 20; j = 10; } }
final
The keyword final
can have one of three meanings:
- to define named constants (variables whose values can’t change after initialization)
- to prevent a method from being overridden
- to prevent a class from being inherited
Be careful when making an object reference final, example below
class MyClass { int a; int b; public MyClass() { a = 2; b = 3; } }
final MyClass object1 = new MyClass(); MyClass object2 = new MyClass();
The reference variable object1
is indeed final
and it’s value can’t change, but what does that mean for reference variables anyway? It means that object1
can’t change which object it is pointing to anymore, but we can change the object itself.
// object1 = object2; // Illegal! object1.a = 5; // Perfectly fine
Preventing Overriding
If you specify the final
modifier while defining a method, any future subclass can’t override it.
class FinalExample { final void printSomething() { System.out.println("Something"); } }
class ExtendsFinalExample extends FinalExample { // This would cause a compile-time error //void printSomething() { // System.out.println("Some other thing"); //} // However, you are perfectly free to overload this method void printSomething(String something) { System.out.println(something); } }
One small bonus of declaring truly final methods as final
is a slight performance boost whenever we call this method. Usually, Java resolves method calls dynamically at run-time, but with methods declared final
, Java can resolve a call to it at compile time, or if a method is really small it can simply inline calls to that method since it “knows” that it won’t be overridden. This eliminates the overhead associated with a method call.
Preventing Inheritance
This usage of final
is fairly straight-forward, a class defined with final
cannot be inherited. This of course implicitly declares all methods of that class final as well (they can’t be overridden if the class can’t be inherited in the first place).
abstract
- abstract non access modifier can be applied on class and methods.
- abstract means that the class or method is not concrete and we need to implement its complete functionality.
- if a class has an abstract method then the class itself is also abstract and it has to be overridden in a concrete class which would extend the abstract class.
synchronized
- When two or more threads need to use the same resource, we somehow need to make sure that only one of them has access to it at a time, i.e. we need to synchronize them.
transient
To do :
- assert
- const*
- continue
- default
- enum
- goto*
- instanceof
- native
- strictfp
- transient
- volatile
Reference :