2. Nested classes
• A class declared inside a class is known as
nested class.
• a class within a class
• group classes in one place
• It can access all the members of outer class
including private members.
• It is used to develop more readable and
maintainable code
3. Syntax of Nested class
class Outer_class_Name{
...
class Nested_class_Name{
...
}
...
}
Outer_Class_name.Nested_class_Name
obj=oc.new Nested_class_Name();
4. class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}
public class MyMainClass {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
5. Object as Parameter
• A method can take an objects as a parameter.
• pass-by-reference
6. class Add
{ int a; int b;
Add(int x,int y)// parametrized constructor
{ a=x;
b=y;
}
void sum(Add A1) // object 'A1' passed as parameter in function 'sum'
{ int sum1=A1.a+A1.b;
System.out.println("Sum of a and b :"+sum1);
}
}
public class classExAdd
{ public static void main(String arg[])
{
Add A=new Add(5,8);
/* Calls the parametrized constructor
with set of parameters*/
A.sum(A);
}
}
Output : Sum of a and b :13
7. class Test1
{
int a,b,c;
}
class Test
{
void get(Test1 obj,int a1, int b1 )
{ obj.a=a1;
obj.b=b1;
obj.c=obj.a+obj.b;
System.out.println(obj.c);
}
}
class Testmain
{
public static void main(String ss[])
{
Test1 tt1=new Test1();
Test tt=new Test();
tt.get(tt1,120,30);
}
}
Object as Parameter
8. passing arguments to methods
Pass by Value:
class Operation{
int data=50;
void change(int data){
data=data+100;//changes will be in the local variable only
}
public static void main(String args[]){
Operation op=new Operation();
System.out.println("before change "+op.data);
op.change(500);
System.out.println("after change "+op.data);
}
}
Output: before change 50
after change 50
Call by Value means calling a method with a
parameter as value
9. class Operation2{
int data=50;
void change(Operation2 op){
op.data=op.data+100;//changes will be in the instance variable
}
public static void main(String args[]){
Operation2 op=new Operation2();
System.out.println("before change "+op.data);
op.change(op);//passing object
System.out.println("after change "+op.data);
}
}
Output:
before change 50
after change 150
10. pass-by-reference
class Point {
public int x, y;
}
public class Tester {
public static void updatePoint(Point point) {
point.x = 100;
point.y = 100;
}
public static void main(String[] args) {
Point point = new Point();
System.out.println("X: " +point.x + ", Y: " + point.y);
updatePoint(point);
System.out.println("X: " +point.x + ", Y: " + point.y);
}
}
Output:
X: 0, Y: 0
X: 100, Y: 100
11. Returning Objects from Methods
class ObjectPassDemo
{ int a, b;
ObjectPassDemo(int i, int j)
{ a = i; b = j; }
boolean equalTo(ObjectPassDemo o)
{ return (o.a == a && o.b == b); }
}
public class Test
{ public static void main(String args[])
{ ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
}
}
Output:
ob1 == ob2: true
ob1 == ob3: false
A method can return any type of data,
including class types
13. Recursion
• Recursion is the technique of making a
function call itself.
Syntax:
returntype methodname()
{
//code to be executed
methodname(); //calling same method
}
14.
15. Factorial of a Number Using Recursion
public class RecursionExample
{
static int factorial(int n)
{
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String[] args)
{
System.out.println("Factorial of 5 is: "+factorial(5));
}
}
Output:
Factorial of 5 is: 120
16. Classification of methods
1)Based on nature of creation – a. user defined
b. predefined
2)Based on return type – a. return a value
b. Return void
3)Based on method call – a. nonrecursive
b. recursive
17. Command-Line Arguments
• It is an argument that is passed at the time of
running the java program
• The arguments passed from the console can
be received in the java program and it can be
used as an input.
18. class CommandLine {
public static void main(String args[]) {
for(int i=0; i<args.length; i++)
System.out.println("args[" + i + "]: " + args[i]);
}
}
• Try executing this program, as shown here:
java CommandLine this is a test 100 -1
Output
args[0]: this
args[1]: is
args[2]: a
args[3]: test
args[4]: 100
args[5]: -1