Initialization & Cleanup

Guaranteed initialization with the constructor
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. 
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.
 
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. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值