Session 3 Notes
Session 3 Notes
Static Members
1
Constructors
Java allows objects to initialize themselves when
they are created through the use of a constructor.
3
Program for constructor
class Box { class BoxDemo1 {
double width; public static void main(String args[]) {
double height; // declare, allocate, and initialize Box objects
double depth;
// This is the constructor for Box. Box mybox1 = new Box(); //constructor invoked
Box() { Box mybox2 = new Box(); //constructor invoked
System.out.println("Constructing Box");
width = 10; double vol;
height = 10; // get volume of first box
depth = 10; vol = mybox1.volume();
} System.out.println("Volume is " + vol);
// compute and return volume
double volume() { // get volume of second box
return width * height * depth; vol = mybox2.volume();
} System.out.println("Volume is " + vol);
} }
}
4
Parameterized Constructors
class Box { class BoxDemo2 {
double width; public static void main(String args[]) {
double height; // declare, allocate, and initialize Box objects
double depth;
// This is the constructor for Box. Box mybox1 = new Box(10, 20, 15);
Box(double w, double h, double d) { Box mybox2 = new Box(3, 6, 9);
width = w;
height = h; double vol;
depth = d;
} // get volume of first box
// compute and return volume vol = mybox1.volume();
double volume() { System.out.println("Volume is " + vol);
return width * height * depth;
} // get volume of second box
} vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
5
Exercise
6
Method Overloading
Two or more methods within the same class that have the
same name but methods must differ in the type and/or
number of their parameters.
7
Example
class OverloadDemo { class Overload {
void test() public static void main(String args[])
{ {
System.out.println("No parameters");
} OverloadDemo ob = new OverloadDemo();
void test(int a) double result;
{ // call all versions of test()
System.out.println("a: " + a); ob.test();
} ob.test(10);
void test(int a, int b) ob.test(10, 20);
{ result = ob.test(123.2);
System.out.println("a and b: " + a + " " + b); System.out.println("Result of ob.test(123.2): " + result);
} }
double test(double a) }
{
System.out.println("double a: " + a);
return a*a;
}
} 8
Constructor Overloading
Similar to method overloading we can also overload
constructor .
9
Example
class Box {
class OverloadCons {
double width;
public static void main(String args[]) {
double height;
// create boxes using the various constructors
double depth;
Box mybox1 = new Box(10, 20, 15);
// constructor used when all dimensions specified
Box mybox2 = new Box();
Box(double w, double h, double d) {
Box mycube = new Box(7);
width = w;
double vol;
height = h;
// get volume of first box
depth = d;
vol = mybox1.volume();
}
System.out.println("Volume of mybox1 is " + vol);
// constructor used when no dimensions specified
// get volume of second box
Box() {
vol = mybox2.volume();
width = -1; // use -1 to indicate
System.out.println("Volume of mybox2 is " + vol);
height = -1; // an uninitialized
// get volume of cube
depth = -1; // box
vol = mycube.volume();
}
System.out.println("Volume of mycube is " + vol);
// constructor used when cube is created
}
Box(double len) {
}
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
} 10
Exercise
11
Using Objects as Parameters
class Test {
int a, b; class PassOb
Test(int i, int j) { {
a = i; public static void main(String args[])
b = j; {
} Test ob1 = new Test(100, 22);
void add(Test o) { Test ob2 = new Test(100, 22);
if(o.a == a && o.b == b) Test ob3 = new Test(-1, -1);
System.out.println(" Equal”);
ob1.equals(ob2));
else
System.out.println(" Not Equal”); ob1.equals(ob3));
} }
} }
12
Returning Object
class Test { class RetOb {
int a ,b; public static void main(String args[]) {
Test() { Test ob1 = new Test(2 ,4);
a = 0;
b=0; Test ob2;
} ob2 = ob1.add();
Test(int i ,int j) { System.out.println("ob1.a: " + ob1.a);
a = i; System.out.println("ob1.b: " + ob1.b);
b=j;
} System.out.println("After Addition :");
Test add()
{ System.out.println("ob2.a: " + ob2.a);
Test temp = new Test(); System.out.println("ob2.b: " + ob2.b);
temp.a=a+10; }
temp.b=b+10; }
return temp;
}} 13
static Members
Normally a class member must be accessed only with an
object.
When a member is declared static, it can be accessed before
any objects of its class are created, and without reference to
any object.
There can be a static method, static variable and a static block
When objects of class are declared, no copy of a static
variable is made.
Instead, all instances of the class share the same static
variable.
Static methods can only call other static methods
Static methods must only access static data
Static block gets executed exactly once, when the class is first
loaded.
14
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) { OUTPUT
System.out.println("x = " + x);
System.out.println("a = " + a);
Static block initialized.
System.out.println("b = " + b); x = 42
}
static { a=3
System.out.println("Static block b = 12
initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}}
15
Outside of the class in which they are defined, static methods and
variables can be used independently of any object.
To call a static method and variable from outside its class, class name is
used .
General form:
classname.method( );
Classname.variable1;
16
class StaticDemo {
static int a = 42; output :
static int b = 99; a = 42
static void callme() {
System.out.println("a = " + a); b = 99
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
17