Introduction
This essay is about JavaSE basic knowledge, while I’m prepare an interview for my next job opportunity. Hence, I wrote it to review for myself. All the content is short summary from 《Core Java》, Cay S. Horstmann
Final Instacne Fields
You can define an instance field as final. Such a field must be initialized when the object is constructed. Afterwards, the field may not be modified again.
here is simple sample.
class sample{
private final String sample_name;
}
The final modifier is particulary useful for fields whose type is primitive or an immutable class. A class is immutable if none of its methods ever mutate its objects. eg. the String class is immutable
Static Keyword
here is for useage of static.
Static Fields
If you define a field as static, then there is only one such field per class. In contrast, each object has its own copy of nonstatic instance fields.
here is simple sample
class sample{
//All objects from this class shared same field variable.
private static int nextId = 1;
}
Static Constants
We can use static combined with final to construct a static constants.
public class Math{
public static final double PI = 3.141519;
}
You can access this constant in your programs as Math.PI.
Static Methods
Static methods are methods that do not operate on objects.
Math.PI();
Factory methods
A common useage of static methdos is for factory methods. A static factory methods that construct objects.
Inheritance
When designing classes, you place the most general methods in the superclass and more specialized methods in its subclasses.