Tuesday, August 17, 2010

Object Oriented Programming (OOP) Aug 17th


Returning an object
================

class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}



class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);
}
}





The output generated by this program is shown here:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22




Demonstrate static variables, methods, and blocks.
======================================


class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}




Here is the output of the program:
Static block initialized.
x = 42
a = 3
b = 12




Demonstrate an inner class.
======================

class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}


Output from this application is shown here:
display: outer_x = 100


Final Keyboard
============


final int FILE_OPEN=1
final int FILE_EXIT=5

No comments:

Post a Comment

Featured Post 1

notice

Featured Post 2

IETE

Featured Post 6

results

Featured Post 7

WBUT

Featured Post 3

Featured Post 4

Featured Post 5

Featured Post 8