Initialization & Cleanup

Initialization & Cleanup

1.       In java, the class designer can guarantee initialization of every object by providing a constructor. If a class has a constructor, Java automatically calls that constructor when an object is created, before users can even get their hands on it.

2.       The name of the constructor is the same as the name of the class. It make sense that such a method will be called automatically during initialization.

3.       The name of the constructor must match the name of the class exactly.

4.       Constructor arguments provide you with a way to provide parameters for the initialization of an object.

5.       the constructor is an unusual type of method because it has no return value. This is distinctyly different from a void return value, in which the method returns nothing but you still have the option to mike it return something else.

Method overloading

1.       One of the important features in any programming language is the use of names. When you create an object , you give a name to a region of storage. A method is a name for an action..

2.       Method overloading is essential to allow the same method name to be used with different argument types.

Distinguishing overloaded methods

1.       Each overloaded method must take a unique list of argument types.

2.       Even differences in the ordering of arguments are sufficient to distinguish two methods/

3.       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.

Default constructors

1.       If you create a class that has no constructors , the compiler will automatically create a default constructor for you.

The this keyword

1.       Suppose you’re inside a method and you’d like to get the reference to the current object. Since that reference is passed secretly by the compiler, there’s no identifier for it. However, for this purpose there’s a keyword: this. 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.

2.       It’s often used in return statements when you want to return the reference to the current object;

3.       The this keyword is also useful for passing the current object to another method..

Calling constructors from constructors

1.       Normally, when you say this, it is in the sense of “this object” or “ the current object” and by itself it produces the reference to the current object . In a constructor, the this keyword takes on a different meaning when you give it an argument list.

The meaning of static

1.       With the this keyword in mind, you can more fully understand what it means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods. In fact, that’s primarily what a static method is for. It’s as if you’re creating the quivalent of a global method. However, global methods are not permitted in java, and putting the static method inside a class allows it access to other static methods and to static fields.

Cleanup: finalization and garbage collection

1.       Programmers know about the importance of initialization, but often forget the importance of cleanup.

2.       When the garbage collector is ready to release the storage used for your object, it will first call finalize(), and only on the next garbage-collection pass will it reclaim the object’s memory. So if you choose to use finalize(), it gives you the ability to perform some important cleanup at the time of garbage collection.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值