mt311 java application development and programming languages

35
MT311 Java Application Development and Programming Languages Li Tak Sing( 李李李 )

Upload: verdad

Post on 06-Jan-2016

38 views

Category:

Documents


0 download

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

Page 1: MT311 Java Application Development and Programming Languages

MT311 Java Application Development and Programming

Languages

Li Tak Sing(李德成 )

Page 2: MT311 Java Application Development and Programming Languages

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.

Page 3: MT311 Java Application Development and Programming Languages

Default Constructors

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

Page 4: MT311 Java Application Development and Programming Languages

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.

Page 5: MT311 Java Application Development and Programming Languages

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);

}

}

Page 6: MT311 Java Application Development and Programming Languages

Default Constructors

The output of the above program is:

0 0.0 false null

Page 7: MT311 Java Application Development and Programming Languages

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);

}

}

Page 8: MT311 Java Application Development and Programming Languages

Default Constructors

The output of the above program is:

1 2.0 true null

Page 9: MT311 Java Application Development and Programming Languages

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.

Page 10: MT311 Java Application Development and Programming Languages

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.

Page 11: MT311 Java Application Development and Programming Languages

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;

}

}

Page 12: MT311 Java Application Development and Programming Languages

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.

Page 13: MT311 Java Application Development and Programming Languages

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;

}

}

Page 14: MT311 Java Application Development and Programming Languages

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.

Page 15: MT311 Java Application Development and Programming Languages

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.

Page 16: MT311 Java Application Development and Programming Languages

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.

Page 17: MT311 Java Application Development and Programming Languages

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.

Page 18: MT311 Java Application Development and Programming Languages

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.

Page 19: MT311 Java Application Development and Programming Languages

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.

Page 20: MT311 Java Application Development and Programming Languages

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.

Page 21: MT311 Java Application Development and Programming Languages

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.

Page 22: MT311 Java Application Development and Programming Languages

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.

Page 23: MT311 Java Application Development and Programming Languages

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.

Page 24: MT311 Java Application Development and Programming Languages

Packages

So actually, the above statement can be changed as:

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

Page 25: MT311 Java Application Development and Programming Languages

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.

Page 26: MT311 Java Application Development and Programming Languages

Packages

Consider the following java file:

package abc.def;

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

Page 27: MT311 Java Application Development and Programming Languages

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

Page 28: MT311 Java Application Development and Programming Languages

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.

Page 29: MT311 Java Application Development and Programming Languages

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.

Page 30: MT311 Java Application Development and Programming Languages

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.

Page 31: MT311 Java Application Development and Programming Languages

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.

Page 32: MT311 Java Application Development and Programming Languages

Packages

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

Page 33: MT311 Java Application Development and Programming Languages

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.

Page 34: MT311 Java Application Development and Programming Languages

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

Page 35: MT311 Java Application Development and Programming Languages

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.