You are on page 1of 20

Maharaja Agrasen Institute of

Technology
CIC-212
Java Programming

Constructors
Constructors
• a special kind of method to construct an instance
of an object from a class template
• default constructor is ClassName()
– created if the class has no constructors
• instance fields are initialized to default values
automatically (see next slide)
• overloaded constructors: same name with
different parameter lists
• use of “this” to call another constructor
Constructors
Java automatically initializes class (static) and object
(instance) fields

Primitive Data Type Default Value


boolean false
int, etc. 0

Object Reference Type null


- not yet referring to an object
Constructors
Constructors
this keyword
Referencing the current object
To call another constructor
Constructors
Constructor initializes an object's instance fields
class MyClass {
int maximum;
int counter;
boolean isBelowMax = true; // override default

MyClass(int maximum) {
this.maximum = maximum;
}

MyClass() { this(100); } // calls this class's constructor

MyClass(int maximum, int counter) {


this.maximum = maximum; // assign local field value
this.counter = counter; // to this object's instance
isBelowMax = counter < maximum;
}
Method Overloading
• overloading is supported:
same method name, different parameter lists

• parameters passed by value, i.e. by copy


– primitive data types and object reference types
– copy of object reference is not a copy of the object

• type of primitive/object returned or void


Classes and Objects
Four Levels of Access to a Class or object's
members
private: accessible from within this class only
 should be standard practice on instance fields
to support OO encapsulation
default (if you don't specify):
accessible from any class in the package
protected: accessible from any subclass or any class in
the package
public: accessible from any class anywhere
Classes and Objects
Access Control Levels
Private
Access Location Public Protected Default private
protected
Same class Yes Yes Yes Yes Yes
Subclass in same pkg. Yes Yes Yes Yes
No

Other classes in same


pkg. Yes Yes Yes No
No

Subclass in other Pkg.


Yes Yes Yes No
No

Non-subclasses in Yes No No No No
other Pkg.
Static keyword
When objects of its class are declared, no copy of a static
variable is made
 Instead, all instances of the class share the same static variable
When a member is declared static, it can be accessed before
any objects of its class are created, and without reference to any
object
The most common example of a static member is main().
 Outside of the class in which static methods are defined, static
methods and variables can be used independently of any object.
To do so, you need only specify the name of their class
followed by the .(dot) operator
ClassName.methodName()
Static keyword
Restrictions:

1. Static methods can only call other static methods.


2. They can only access static data.
3. They can not refer to ‘this’ or ‘super’ in any way.
Static Blocks
• Static block is a block of code prefixed by ‘static’ keyword
• Gets executed only once
• Used to initialize static variables
class A {
static int x;
static {
x = 10; static block
initializes
} x to 10

public static void main (String args[])


{
System.out.println (“ x value is : ”+ x);
}
Output : x value is : 10
}
Example Program : Static Block
class A {
static int x = 10;
static int y; Output
static void call( int p) { x value is : 10
System .out. println(“ x value is :”+x); y value is : 20
System .out .println(“ y value is :”+y); p value is : 30
System .out .println(“ p value is :”+p);
}
static {
System.out.println(“ static block initialized”);
y=x*2;
}
public static void main (String args[]) {
call(30);
}
}
Final keyword
A variable can be declared as final

Doing so prevents its contents from being modified

This means that you must initialize a final variable


when it is declared

Permits us to create typed constants

 In usage, final is similar to const in C / C++


Example final variables

Syntax
final type constName = value;

Example
final int FILE_NEW = 1;
final float PI = 3.141519;
Final Variables
Subsequent parts of the program may use the above
final variables FILE_NEW and PI

It is common coding convention to choose all uppercase


identifiers for final variables

Variables declared as final do not occupy memory on a


per-instance basis

Thus, a final variable is essentially a constant


Example program : final variables
Correct program Wrong program

class A { class B {
final int X = 10; final int X = 10;
public static void main (String args[]) public static void main(String args[]) {
{ X = 20;
System.out.println(“ X is “+X); System .out. println(“ X is “+X);
} }
} }

final variable
should not change
Other Uses Of final

The keyword final can also be applied to


methods,
Its meaning is substantially different than when it
is applied to variables
Varargs

 Newly implemented feature in Java 5.0


 Basic syntax
type … variableName
 Argument passed to a method is converted
into an array of the same-typed values
sum (10,20) sum(new int[] {10,20})
Varargs
Example
public static void main(String[] args)
{
System.out.println(“The sum is ” + sum(10,20,30));
}
public int sum (double … numbers)
{
int total = 0;
for (int i = 0; i < numbers.length; i++)
total += numbers[i];
return total;
} OUTPUT:
The sum is 60
Varargs
• Varargs is done with the … operator.

• Example. The following constructor


public Person (String name, String details… )

Can be called with many different invocations:

new Person (“Alexander ”);


new Person (“Alexander ”, “Bell ”);
new Person (“Alexander ”,”Graham”, “Bell ”);

You might also like