Docstoc

Java Chapter 3

Document Sample
Java Chapter 3 Powered By Docstoc
					              CHAP-3 More about Methods and Classes

1)) overloading methods
 In java it is possible to define two or more methods within the same class
  that share the same name, but their parameter declaration and data type
  are different.

 This method is said to overload and this process is referred to as method
  overloading.

 Overloaded methods are different from one another by number and type of
  the arguments passed into the method and return type of method.

 At the runtime, java uses the type and numbers of argument to call the
  overloaded method. It also uses return type of method.

 Only return type of overloaded method is not sufficient to differentiate the
  methods.

 Type and number of arguments of overloaded methods must differ.

EXAMPLE:-

class first
{
   int a,b;

  void test( )  //no parameter passed in test()
  {
    System.out.println("NO PARAMETERS");
  }

  void test(int i)     //one parameter passed in test()
  {
    a=i;
    System.out.println("a=" + a);
  }
  void test(int i,int j)    //two parameter passed in test()
  {
    a=i;
    b=j;
    System.out.println("a=" + a + "b=" + b);
  }
}
public class DEMO
{
  public static void main(String args[])
  {
    first f1=new first();
    f1.test();
    f1.test(10);
    f1.test(10, 20);
  }
}

OUTPUT
NO PARAMETERS
a=10
a=10b=20

2)) Object as a Function Argument
 We can also pass objects as a parameter in JAVA programming.

EXAMPLE:-

class first
{
   int a,b;

  public void getdata(int i,int j)
  {
    a=i;
    b=j;
  }
    public boolean equals(first o)           //Object as a function argument
    {
      if(o.a==a && o.b==b)
      {
         return true;
      }
      else
      {
         return false;
      }
    }
}

public class DEMO
{
  public static void main(String args[])
  {
    first f1=new first();
    first f2=new first();
    first f3=new first();

        f1.getdata(10, 10);
        f2.getdata(10, 10);
        f3.getdata(1, 1);

        System.out.println("f1 == f2:" + f1.equals(f2));
        System.out.println("f2 == f3:" + f2.equals(f3));
        System.out.println("f3 == f1:" + f3.equals(f1));
    }
}
OUTPUT

f1 == f2: true
f2 == f3: false
f3 == f1: false
3)) Returning Objects
 A method can return any type of data, including class types that you
  create.

 So, we can also return objects because it is a class variable.

class first
{
   int a;
   first( int i) //constructor
   {
      a=i;
   }
    first inc()     //returning objects
   {
      first temp =new first(a+10);
      return temp;
   }
}

public class DEMO
{
  public static void main(String args[])
  {
    first f1=new first(10);

        System.out.println("f1.a:" + f1.a);

        first f2;

        f2=f1.inc();
        System.out.println("After first increase f2.a:" + f2.a);

        f2=f2.inc();
        System.out.println("After second increase f2.a:" + f2.a);
    }
}
OUTPUT

f1.a:10
After first increase f2: a:20
After second increase f:. a:30

4)) Recursion
 Java supports recursion. Recursion is the process that allows methods to
  call itself.
 A method that calls itself is said to be recursive.

 When a method call itself, new local variables and parameters are
  allocated storage on that stack and the method code is executed with these
  new variables from the start.
 As each recursive call returns, the old variables and parameters are
  removed from the stack and execution restarts at the point of the call inside
  the method.

 The main advantage to recursive methods is that they can be used to create
  clear and simple versions of several algorithms.

 When writing recursive methods, you must have if statement that returns
  method without recursive call.

 If you don’t have if statement for return method then recursive method
  goes in the infinite loop.

EXAMPLE:-

class first
{
   int fact( int n)
   {
     int ans;
     if(n==1)
     {
        return 1;
     }
        else
        {
           ans=fact(n-1)*n;    //Recursion
           return ans;
        }
    }
}

public class DEMO
{
  public static void main(String args[])
  {
    first f1=new first();

        System.out.println("factorial of 3 is:" + f1.fact(3));
        System.out.println("factorial of 4 is:" + f1.fact(4));
        System.out.println("factorial of 5 is:" + f1.fact(5));
    }
}


OUTPUT

factorial of 3 is:6
factorial of 4 is:24
factorial of 5 is:120


5)) Static Keyword
 There will be times when you will want to define a class member that will
  be used independently of any object of that class.

 Normally, a class member must be accessed only with object of its class.

 However it is possible to create a member that can be used by itself without
  reference to a specific object.

 To create such a member, declare it as a static.
 When a member is declared static, it can be accessed before any objects of
  its class are created and without reference to any object.

 The most common example of a static member is main (). Main () is
  declared as static because it must be called before any objects exist.

 Java creates only one copy of a static variable. All the instance variables
  and methods share the same static variable.

 Static method will be called without using object.

 Static methods have several restrictions.

1)) they can only call other static methods.

2)) they must only access static data.

3)) they cannot refer to this or super keyword in any way.

EXAMPLE-1:-

class first
{
   static int a=10; //static variable
   static int b=20;

  static void display()      //static method
  {
     System.out.println("a=" + a);
  }
}
public class DEMO
{
  public static void main(String args[])
  {
    first.display();
    System.out.println("b=" + first.b);
  }
}
OUTPUT

a=10
b=20

EXAMPLE-2:-

class DEMO
{
   static int a=10; //static variable
   static int b;
   int c;     //instance variable

    static void display(int x)            //static method and x is local variable
    {
       System.out.println("x=" + x);
       System.out.println("a=" + a);
       System.out.println("b=" + b);
       //System.out.println("c=" + c);
       //c is instance variable so it can't access in static method
    }
    static
    {
       System.out.println("first static block initialized");
       b=a+10;
    }
    static
    {
       System.out.println("second static block initialized");
       b=b*10;
    }

    public static void main(String args[])
    {

        display(5);
    }
}
OUTPUT
first static block initialized
second static block initialized
x=5
a=10
b=200

6))Introducing Nested and Inner Classes

class A     //enclosing
{
      class B      //nested class
      {
            …..
      }
}

 It is possible to define a class within another class, such classes are known
  as Nested Classes

 The scope of a nested class is bounded by the scope of its enclosing class.

 Thus if class B is defined within class A, then B does not exist outside of A.

 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 the members of nested
  class.

 There are two types of nested class.
  1) Static
  2) Non-static

   1) static nested class
          A static nested class is one which has the static modifier applied.
           Because it is static, it must access the members of its enclosing
           class through an object.

         That is, it cannot refer to members of its enclosing class directly.
   2) non-static nested class
         An inner class is a non static nested class.

          It has access to all of the variables and methods of its outer class
           and may refer to them directly in the same way that other non-
           static members of the outer class do.

7)) Command-Line Arguments
 Sometimes you want to pass information into a program when you run it.

 This is accomplished by passing command-line argument to main().

 A command-line argument is the information that directly follows the
  program’s name on the command line when it is executed.

 To access the command-line arguments inside a java program:-

 We want to stored them as string in a string array passed to the args
  parameter of main().

 The first command-line argument is stored as args[0], the second at args[1]
  and so on.

EXAMPLE:-

public class DEMO
{
  public static void main(String args[])
  {
    for(int i=0;i<args.length;i++)
    {
       System.out.println("args["+ i +"]:" + args[i] );
    }
  }
}
OUTPUT

Try executing this file into command prompt
Using java command as shown here:::-

java DEMO this is a test program for command line

				
DOCUMENT INFO
Shared By:
Categories:
Tags: java, help
Stats:
views:16
posted:4/28/2012
language:
pages:11
Description: More about Methods and Classes