Guaranteed initialization with the constructor
lives either on the stack or in static storage.
Distinguishing overloaded methods
There’s a simple rule:
Each overloaded method must take a unique list of argument types
. 方法参数个数相同,但是参数类型排序不同也可以。
If your argument is wider, then you must
perform a narrowing conversion with a cast. If you don’t do this, the compiler will issue an
error message.
perform a narrowing conversion with a cast. If you don’t do this, the compiler will issue an
error message.
You cannot use return value types to distinguish overloaded methods.
Default constructors
The this keyword
The this keyword—
which can be used only inside a non-static method—produces the reference to the object that the method has been called for.
Calling constructors from constructors
While you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you’ll get a compiler error message.
The compiler won’t let you call a constructor from inside any method other than a constructor.
Cleanup: finalization and garbage collection
1. Your objects might not get garbage collected.
2. Garbage collection is not destruction.
3. Garbage collection is only about memory.
You must perform cleanup
Remember that neither garbage collection nor finalization is guaranteed. If the JVM isn’t close to running out of memory, then it might not waste time recovering memory through garbage collection.
How a garbage collector works
(1)reference counting.
(2)In faster schemes, garbage collection is not based on reference counting. Instead, it is based
on the idea that any non-dead object must ultimately be traceable back to a reference that
lives either on the stack or in static storage.
(3)adaptive garbage-collection scheme
- stop-and-copy
- mark-and-sweep
“Adaptive generational stop-and-copy mark-andsweep.”
Member initialization
Order of initialization
Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructor.
static
data initialization
There’s only a single piece of storage for a static, regardless of how many objects are created. You can’t apply the static keyword to local variables, so it only applies to fields. If a field is a static primitive and you don’t initialize it, it gets the standard initial value for its type. If it’s a reference to an object, the default initialization value is null.
在public类中的static语句块要比main方法先执行,但是静态方法除外。
You should generally only use a variable argument list on one version of an overloaded
method. Or consider not doing it at all.
method. Or consider not doing it at all.
Enumerated types
Although
enums appear to be a new data type, the keyword only produces some compiler behavior while generating a class for the
enum, so in many ways you can treat an
enum as if it were any other class. In fact, enums are classes and have their own methods.