2 constructor

19
Constructor

Upload: ita-destiny

Post on 08-Nov-2014

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2 Constructor

Constructor

Page 2: 2 Constructor

ConstructorConstructor is a special method in java class.

It is used to initialize a new object variables.

A constructor has the same name as the class. For example if you have Color class the constructor of color class is also Color( );

Page 3: 2 Constructor

Constructors cannot return any value not even void.

If we are not providing any constructor for a class than default (no parameter) constructor is automatically provided by the runtime system.

When we will create our own parameterize constructor. We have to create our own default constructor as well because then default constructor is not available.

The constructor can be private, public or protected.

Page 4: 2 Constructor

Java supports name overloading of constructors so that a class can have number of constructors with the same name.

The compiler will determine at run time which constructor to call by matching the number and type of arguments you are passing into the constructor.

Page 5: 2 Constructor

this

Java this keyword is used to invoke any method or variable of current class.

The syntax of method is: -

this.methodname( );The syntax of variable is: -

this.variablename = 7;

Page 6: 2 Constructor

If you want to invoke another constructor of current class inside constructor you can use this statement as follows:

this(4, 5);The above statement will invoke the current class constructor that takes two int arguments.

Call to current class constructor with this would be the first statement in the constructor.

Page 7: 2 Constructor

DestroyingObjects(Garbage Collection)

Page 8: 2 Constructor

Some programming languages required that you keep track of all the objects you create and then you explicitly destroy them when they are no longer needed.

The Java platform allows you to create as many objects as you want and you don't have to worry about destroying them.

Object Destruction & Garbage Collection

Page 9: 2 Constructor

The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is eligible for garbage collection when there are no more references to that object for example if object goes out of scope.

You can explicitly drop an object reference by setting the variable to the special value null.

Page 10: 2 Constructor

If you want to invoke garbage collector in the program you can do this by issuing following commands.

System.gc(); Runtime.getRuntime().gc();

Page 11: 2 Constructor

finalize() method

If you want an object to clean up its state before it is deleted from the memory, you can declare finalize() method in the class.

This method will be called by the garbage collector before deleting any object of this class.

Page 12: 2 Constructor

This method is inherited from the parent Object class in all java classes and can be overridden like the following code:

protected void finalize(){

super.finalize(); // clean up code here.

}

Page 13: 2 Constructor

ClassVariables

(static variables)

Page 14: 2 Constructor

The runtime system allocates class variables once per class regardless of the number of instances created of that class.

The system allocates memory for class variables the first time it encounters the class at class load time.

All instances share the same copy of the class variables.

You can access class variables through an instance or through the class itself.

Class Variables

Page 15: 2 Constructor

To declare a class variable follow the following syntax.

static Type variable;e.g.static int a;static double d;

If any object change the value of class variable, the change will effect the values of all the variables.

Page 16: 2 Constructor

Because the class variable belongs to the class rather than any specific variable, it can be accessed using the class name such as: -

ClassName.variableName;

Suppose you have class called Car and class variable called gears than you can access that variable like this: -

Car.gears;

Page 17: 2 Constructor

Class Variablevs.

Instance Variable

Page 18: 2 Constructor

All objects have their own copy of instance variable.

All objects share the single copy of static variable.

Instance variable is declared without static keyword.

int a = 4;

Class variable is declared by using static keyword.

static int a = 4;

Instance variableClass variable

Class vs. Instance Variables

Page 19: 2 Constructor

Instance variable cannot be accessed without creating an object.

ObjectName.variable

Static variable can be accessed without creating an object, by using class name.

ClassName.variable

Instance variable depends on the object for which it is available.

Static variable does not depend on the single object because it belongs to a class.