unit 2
unit 2
unit 2
Unit-2
Datatype instance_variable;
datatype methodname1(parameter_list)
{
//body of mehod
}
datatype methodname2(argument_list)
{
//body of method
}
}
Heap Stack
E.g.
null name
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 22
class Box //define a class
{ private :
int length;
int breadth;
int height;
public :
void setdata(int l, int b, int h) //member function to set data
{ length = l; breadth = b; height = h; }
void showdata() //member function to display data
{ System.out.println(“length =“+length+ “breadth =“+breadth+”height
=“+height);}
public static void main( String args[])
{ Box b1 = new Box();
b1.setdata(10,20,30);
b1.showdata();
}
} Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 23
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University
24
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University
25
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University
26
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University
27
Constructors
1) Default Constructor
This constructor is generated by the compiler when the
user don’t write any constructor
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output :
22
33
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 40
2) Method Overloading: changing data type of arguments
class Adder{
static int add(int a, int b){
return a+b;
}
static double add(double a, double b){
return a+b;
}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Output
22
24.9
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 41
3) Method Overloading: changing sequence of arguments
class Adder{
static float add(int a, float b){
return a+b;
}
static float add(float a, int b){
return a+b;
}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11.0)); //method is defined as static
System.out.println(Adder.add(12.3,12));
}}
Output
22.0
24.3
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 42
• Can we overload java main() method?
• Yes, by method overloading. You can have any number of
main methods in a class by method overloading.
But JVM calls main() method which receives string array as
arguments only.
class TestOverloading{
public static void main(String[] args){
System.out.println("main with String[]");}
public static void main(String args){
System.out.println("main with String");}
public static void main(){
System.out.println("main without args");}
}
• Output:
main with String[]
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 43
Returning a value
• Method returns value
class A{
A(){
this(5);
System.out.println("hello a");
}
A(int x){
System.out.println(x);
}
}
class Test{
public static void main(String args[]){
A a=new A();
}}
Output : 5
hello a
Counter(){ Counter(){
count++;//incrementing value count++;//incrementing value
System.out.println(count); System.out.println(count);
} }
public static void main(String args[]){ public static void main(String args[]){
//Creating objects //Creating objects
Counter c1=new Counter(); Counter c1=new Counter();
Counter c2=new Counter(); Counter c2=new Counter();
Counter c3=new Counter(); Counter c3=new Counter();
} }
} }
Output: Output:
1 1
1 2
1 3
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
} }
Output : Compile Time Error
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 65
2) Java static method
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
//Java Program to demonstrate the use of a static method. //Test class to create and display the values of
object
class Student{ public class TestStaticMethod{
int rollno;
String name; public static void main(String args[]){
static String college = “ITM";
Student.change();//calling change method
//static method to change the value of static variable //creating objects
static void change(){
college = “ITM SLS"; Student s1 = new Student(111,"Karan");
} Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//constructor to initialize the variable //calling display method
Student(int r, String n){
rollno = r; s1.display();
name = n; s2.display();
} s3.display();
//method to display values }
|
void display(){
System.out.println(rollno+" "+name+" "+college);}
}
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 66
Restrictions for the static method
class A{
int a=40;//non static
Output:
Compile Time Error
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
• Syntax :
protected void finalize() throws Throwable
Throwable - the Exception is raised by this method
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 78
public class Test {
public static void main(String[] args)
{
Test obj = new Test();
System.out.println(obj.hashCode());
obj = null;
// calling garbage collector
System.gc();
System.out.println("end of garbage collection");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
} }
Output : 2018699554
end of garbage collection
finalize method called
Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 79
Recursion
• Each time a method is called, a new space is allocated on
the internal stack for all the variable in the method, which
means there is no reason you can’t call the same method
again – a new set of variables will be allocated on the stack
automatically
• Recursion is a technique in which a method calls itself.
• This technique provides a way to break complicated
problems down into simple problems which are easier to
solve.
• Examples of recursion are:
• Factorial of number
• Fibonacci series