mt311 java application development and programming languages

Post on 06-Jan-2016

38 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

MT311 Java Application Development and Programming Languages. Li Tak Sing( 李德成 ). Constructor. A constructor is like a method except that it should have the same name as the class; it should have no return type. - PowerPoint PPT Presentation

TRANSCRIPT

MT311 Java Application Development and Programming

Languages

Li Tak Sing(李德成 )

Constructor

A constructor is like a method except that– it should have the same name as the class;– it should have no return type.

A constructor will be called when you use the 'new' keyword to create an instance of a class.

Default Constructors

If you do not provide any constructor, the Java will create a default constructor for it.

Default Constructors

When the default constructor is executed, all its attributes will be initialized according to the following rules:– if the attribute is numerical, like int, byte, float etc., the

attribute is assigned the value of 0.– if the attribute is a class variable, then it is initialized to

"null" which represent that the variable is not referring to any object yet.

– if the attribute is boolean, then it will be assigned the value of false.

Default Constructors

public class ABC {

int a;

float b;

boolean c;

ABC d;

public static void main(String st[]) {

ABC abc=new ABC(); //default constructor

System.out.println(abc.a+" "+abc.b+" "+abc.c+" "+abc.d);

}

}

Default Constructors

The output of the above program is:

0 0.0 false null

Default Constructors

If you want to initialize the attributes to other value, you can do it like this:public class ABC {

int a=1;

float b=2;

boolean c=true;

ABC d=null;

public static void main(String st[]) {

ABC abc=new ABC();

System.out.println(abc.a+" "+abc.b+" "+abc.c+" "+abc.d);

}

}

Default Constructors

The output of the above program is:

1 2.0 true null

Constructors

The above method has a problem in that all instance of ABC would be the same when they are just created, i.e., the attribute a will definitely be 1, the attribute b will be 2.0 etc.

If we want to have the attributes initialized to different values depending on different situations, we need to write our own constructors.

Constructor

A constructor is a piece of code that will be executed when a new instance of class is created.

Just like methods, a constructor can have a number of parameters.

Just like methods, you overload a constructor provided that these constructors have different signatures.

Unlike methods, a constructor cannot have return value.

Constructors

public class ABC {

int a=1;

float b=2;

boolean c=true;

public ABC(int aa) {

a=aa;

}

public ABC(float bb) {

b=bb;

}

public ABC(boolean cc) {

c=cc;

}

}

Constructors

...

ABC a1=new ABC(); //error, now ABC does not have a default

// constructors

ABC a2=new ABC(10); //a2.a=10, a2.b= 2.0, a2.c = true

ABC a3=new ABC(10.0); //a3.a=1, a3.b=10.0, a3.c= true

ABC a4=new ABC(false); //a3.a=1, a3.b=2.0, a3.c=false.

Constructors

Consider a change in ABC:public class ABC {

int a=1;

float b=2;

boolean c=true;

public ABC(float bb) {

b=bb;

}

public ABC(boolean cc) {

c=cc;

}

}

Constructor

...

ABC a1=new ABC(); //error, now ABC does not have a default

// constructors

ABC a2=new ABC(10); //a2.a=1, a2.b= 10.0, a2.c = true

ABC a3=new ABC(10.0); //a3.a=1, a3.b=10.0, a3.c= true

ABC a4=new ABC(false); //a3.a=1, a3.b=2.0, a3.c=false.

Garbage collection

When you create a new object, it takes up the computer's resources. So as you create more and more objects, it is possible that all the computer's resources are used up.

So it is necessary to release those resources that have been taken up by objects that will never be used again.

Garbage Collection

Consider the following code:

for (int i=0;i<10;i++) {

ABC abc=new ABC();

....

}

In each loop, an ABC is created. Assume that once the loop is finished, these ABCs are no longer used and referenced, then the resources taken up by these ABCs can be released to other use.

Garbage collection

In Java, this is done by a process called garbage collection.

From time to time, the process will check whether there are resources that are taken up by objects that are no long used. It will then release the resources back to the system.

Finalize

When the garbage collection process is releasing the resource taken up by an object, it is possible that this object is holding some important information. For example, this object may have an opened file with some contents that have not been written to the file properly. So by releasing the resources held by the object may result in loosing this contents.

Finalize

In order to prevent this from happening, we need to have some code to close the file before the resources are released.

We need to use the finalize method for this purpose.

Finalize

The finalize method must have no parameters and no return type. So it is always declared as:

public void finalize()

When the garbage collector is going to release the resource of an object, it will invoke the finalize method of the object first.

Finalize

So it is the programmer's responsibility to put the code in the finalize method to do whatever it need to do before the resources are released, for example, close a file or close a network connection.

However, in real Java applications, you rarely use the finalize method because it is usually good habit to close a file when you are done with the file. Not to wait until the object is garbage collected.

Packages

A package is a collection of classes. It is very similar to directories in a file system. In a file system, a directory can have a

number of other directories and files. So a package can contain a number of other

packages and classes.

Packages

For example, you have been using the following command many times:

System.out.println("....");

Actually, System is a class defined in Package java.lang. So here, java is a package, lang is also a package that is defined in java and then System is a class defined in lang. Then, out is a static object of the class System. println is a method of the object out.

Packages

So actually, the above statement can be changed as:

java.lang.System.out.println("...");

Packages

The following statement in a java file define the package that the class is in:

package myPackage; When a class is defined in myPackage, then

the class file should be put under a directory called myPackage.

Packages

Consider the following java file:

package abc.def;

public class MyClass { public void method() { .... } static int aStaticVariable=2;}

Packages

After compilation of the file, the MyClass.class file should be put in the following directory:abc/def

When other programs wants to use MyClass, it can be referred to as:

abc.def.MyClass

Packages

If you do not want to use that long name, you can have the following statement:

import abc.def.MyClass;

This statement will tell the compiler where to find MyClass and therefore we can simply refer to the class by using MyClass, not the full name.

Packages

The way of using the import keyword is:

import abc.def.*;

This statement inform the compiler to look for all classes in the package abc.def, not just MyClass as that in the last statement. So all classes defined in the directory abc/def can be used without the need of the full name.

Packages

So, when you want to use a class ABC in a package def.ghi, you can do either of the following ways:– use the full name: def.ghi.ABC– include the statement: import def.ghi.ABC; and

then you can refer to the class as ABC in the program.

– include the statement: import def.ghi.*; and then you can refer to the class as ABC.

Packages

There are three situations where you do not need to specify the package at all:– The code that uses the class is in the same package

of the class.– The class belongs to the package java.lang. This

package hold the most fundamental classes of Java and therefore there is no need to use either the import statement or the full name for classes in that directory.

– The class does not belong to any package.

Packages

That is why when you use System.out.println(".."), you do not need to preceed it with java.lang.

Packages

When you create a project in Netbeans, you will be asked about the package name. Then, usually, when you create a class, the class will be placed in that package.

Where to find a package

We know that when a class ABC is defined in a package def.ghi, then ABC.class should be found in the directory def/ghi. However, how can the Java system knows where is def/ghi? The is done by either using the environment variable called CLASSPATH or you can set the classpath when you run the java command:java -classpath=... ABC

Where to find a package

The other way is that Java will look for the current directory. ABC.class is actually at:

/jkl/mno/def/ghi/ABC.class, Then, in order for the Java interpreter to find

the class, we can set the classpath to /jkl/mno or we run the command at the directory /jkl/mno.

top related