Lect-5 Methods & Classes
Lect-5 Methods & Classes
ob.test(); No parameters
ob.test(10, 20); a and b: 10 20
double vol;
// get volume of clone
vol = myclone.volume();
System.out.println("Volume of clone is " + vol);
}
}
Parameter Passing
• Two types of variables:
1) simple types
2) class types
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
ob.meth(ob);
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
Returning Objects
• A method can return any type of data, including class types.
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}
Returning Objects
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);
}
}
Recursion
• A recursive method is a method that calls itself:
// methods to access c
void setc(int i) { // set c's value
c = i;
}
int getc() { // get c's value
return c;
}
}
Access Control
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
Stack() {
tos = -1;
}
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
Revisiting Stack Class
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
static {
System.out.println("Static block initialized.");
b = a * 4;
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
final
• A field declared as final will prevent its
contents from being modified, making it,
essentially, a constant.
• Two ways to declare
– Give it a value when it is declared
– Assign it a value within a constructor
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
final int FILE_SAVEAS = 4;
final int FILE_QUIT = 5;
Nested & Inner Classes
• Define a class within another class –
Nested class
• If class B is defined within A, then B does
not exist independently of A.
• The scope of a nested class is bounded by
the scope of its enclosing class.
• A nested class has access to the members,
including private members of the class, in
which it is nested.
• However, the enclosing class does not have
access to members of the nested class
Nested & Inner Classes
• Types of nested class – static & non-static
• Static nested class are seldom used
• Non static nested class aka Inner class
• Inner class has access to all of the
variables and methods its outer class and
may refer to them directly in the same way
that other non-static members of the
outer class do.
Nested & Inner Classes
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
void test() {
for(int i=0; i<10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
} display: outer_x = 100
} display: outer_x = 100
} display: outer_x = 100
display: outer_x = 100
class InnerClassDemo { display: outer_x = 100
display: outer_x
public static void main(String args[]) ={ 100
display: outer_x = 100
Outer outer = new Outer();
outer.test(); display: outer_x = 100
} display: outer_x = 100
} display: outer_x = 100
String Handling
• String is probably the most commonly used class in
Java's class library. The obvious reason for this is that
strings are a very important part of programming.
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
} }
Queue class
- having insertion & deletion operations
- Use only constructors
- Define QuizQueue class to use Queue class