Chapter 1
Chapter 1
Controlling Access to Class Members, Pass Objects to Methods, How Arguments are passed, Returning
Objects, Method Overloading, Overloading Constructors, Recursion, Understanding Static, Introducing
Nested and Inner Classes, Varargs: Variable-Length Arguments.
Call-By-Value Call-By-Reference
class Program{ class Test {
static void increase(int p) int data;
{ Test (int d){
p =p+1; data=d;
} }
public static void main(String[]args){ void show(){
int a=5; System.out.println("Data="+data);
increase(5); }
System.out.println(“A = ”+a) }
} class Myprogram{
} static void increase(Test p){
p.data = p.data+1;
}
public static void main(String[] args){
Test p =new Test(5);
p.show(); // Data = 5
Myprogram.increase(p);
p.show(); //Data = 6
}
}
Output: Output:
A =5 Data = 5
Data = 6
class Number{
int num;
Number(int a){
num=a;
}
void change(int a){
a+=5;
}
void change(Number ob){
ob.num +=5;
}
intgerNum() {
return num;
}
}
class DemoArgumentPassing {
public static void main(String[] args){
Number n1 =new Number(10);
int x=20;
n1.change(x); // call-by-value
Output:
Value of x change x = 20
Value of object after change num = 15
Note: In Java, when you pass a primitive type to a method, it is passed by value and Objects
are implicitly passed by using call-by-reference.
class Number {
int num;
Number(int a) {
num = a;
}
Number incrementBy(int a) { //returning obejct
Number temp = new Number(a+num);
return temp; }
int getNum(){
return num; } }
class Demo {
public static void main(String [] args) {
Number n1 = new Number(10);
Number n2 = new Number(10);
N2 = n1.incrementBy(5); //returns object
System.out.printl(“ Value of object calling method = ” + n1.getNum());
System.out.printl(“ Value of object receiving other object = ” + n2.getNum());
} }
Output:
class Number {
Value of object calling method =10
int num;
Value of object receiving other object =15
Constructor Overloading
}
ClassName(Parameter-List) {//Parameterized constructor
// code
}
}
Method Overloading
Method overloading is a way of defining two or more methods with same name but
different forms. It is also known as Static Polymorphism.
Syntax
class ClassName {
//code }
//code }
class Shape {
int volume(int length, int breadth, int height) { // method with parameters
return length * breadth * height;
}
public static void main(String [] args) {
System.out.println(“ --Demonstrating Constructor Overloading-- ”);
2.6 Recursion
Defintion:
Process of calling function itself.
A recursive procedure routine is one that has ability to call itself
Why recursion:
o it requires the least amount of code to perform the necessary functions.
o Solves complicated problems in simple steps
A recursive function has two parts:
o Base case: is stopping condition to prevent an infinite loop
o Recursive case: it must always get closer to base case from one invocation to
another.
Example 1: Factorial of a number
Mathematical definition
Recursive Method
class RecursiveDemo {
int factorial(int number)
{
if (number == 0)
return 1;
else
return (number * factorial(number - 1));
}
public static void main(String [] args) {
Mathematical definition
className.StaticMethod();
Most common example of a static member is main( ).
main() is declared as static because it must be called before any object.
Different uses of static are:
o static variable
o static method
o static block
static variable
o Instance variables declared as static are essentially, global variables.
o When objects of its class are declared,
o No copy of a static variable is made.
o All instances of the class share the same static variable.
o A static variable can be accessed Using dot operator with class name.
Syntax
className.staticVariable
static method
o Restrictions on static method
className. staticmethod( )
class StaticDemo {
static float basic = 5000;
static float DA; Static variable
Execution steps:
Definition
Nested class
classInnerClass{
……
}
}
This syntax tells the compiler that vaTest( ) can be called with zero or more arguments.
As a result, v is implicitly declared as an array of type int[ ].