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.

 

你可能感兴趣的:(Access)