0% found this document useful (0 votes)
17 views19 pages

05 Java Fundamental Method Overloading, Static

Uploaded by

amitchaurasia774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views19 pages

05 Java Fundamental Method Overloading, Static

Uploaded by

amitchaurasia774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

class Student{

int rollno; 1
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
2
Annonymous object

Annonymous simply means nameless. An object that have no reference is known as annonymous
object.
If you have to use an object only once, annonymous object is a good approach
class Calculation{
void fact(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println("factorial is "+fact);
}
public static void main(String args[]){
new Calculation().fact(5);//calling method with annonymous object
}
}
class Rectangle{
int length;
int width; 3
void insert(int l, int w)
{
length=l;
width=w;
}

void calculateArea()
{
System.out.println(length*width);
}
public static void main(String args[]){
Rectangle r1=new Rectangle(),r2=new Rectangle();

r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
4
Method Overloading in Java

If a class have multiple methods by same name but different parameters, it is known as
Method Overloading.

Method overloading is defined as the functionality that enables to define two or more
methods with the same name but with different signatures within the class.

Method overloading increases the readability of the program.


The methods that share the same name, but have different signatures are called overloaded
methods.
The signature of a method consists of:
• The number of arguments it takes.
• The data type of the arguments.
• The order of the arguments.
class Calculator
{ 5
public int multiply(int a, int b)
{
System.out.println("Multiplying two integer values:");
return a * b;
}
public double multiply(double a, double b)
{
System.out.println("Multiplying two double values:");
return a * b;
}
public static void main(String args[])
{
Calculator c=new Calculator();
System.out.println(c.multiply(5,10));
System.out.println(c.multiply(1000.6,1000.5));
}
}
6
Passing Arguments to a Method

• Arguments can be passed to a method using two ways:


• Call-by-value: Copies the value of the actual parameters to the formal
parameters. Therefore, the changes made to the formal parameters have no
effect on the actual parameters.
• Call-by-reference: Passes the reference and not the value of the actual
parameters to the formal parameters in a method. Therefore, the changes made
to the formal parameters affect the actual parameters.
7
Passing Arguments by Value:

• In Java, when arguments of primitive data


type, such as int and float are passed to a
method then they are passed by value. The
following figure shows the concept of passing
arguments by value:
• When arguments are passed by value, a
copy of the actual argument is passed to the
formal arguments of the called method,
which is maintained at a separate memory
location. Therefore, when the called method
changes the value of the argument, the
change is not reflected in the actual
argument.
class PassByValue
{ 8
void Calling()
{
int value =5;
System.out.println("The value of variable is:" +value);
Called(value); // Actual parameters.
System.out.println("The value of variable after change in Called method
is:" + value);
}
void Called(int passedValue) // Formal parameters
{
passedValue = 10;
} }
class PassByValueTest {
public static void main(String args[]) {
PassByValue t =new PassByValue();
t.Calling();
}
}
9
Passing Arguments by Reference:

• Passes a reference to an argument to the


parameter of a method.
• In Java, the objects of abstract data type are
passed by reference.
• When arguments are passed to the method by
reference then any change made to the formal
argument by the called method is also reflected in
the actual argument. The following figure shows
the concept of passing arguments by reference:
• The argument passed by reference to the formal
parameter has the same memory location as that
of the actual parameter.
class RefObject
{ 10
String Name;
}

class Test {
void Calling() {
RefObject RO = new RefObject();
RO.Name= "Original";
System.out.println("The value of variable, Name, is:” + RO.Name);
Called(RO);
System.out.println("The value of variable, Name, after change in Called method is:" +
RO.Name);
}
void Called(RefObject RefObj) {
RefObj.Name = "Changed";
}
}
class PassByRefTest {
public static void main(String args[]) {
Test t =new Test();
t.Calling();
}
11
static keyword

The static keyword is used in java mainly for memory management. We may apply static
keyword with variables, methods, blocks and nested class. The static keyword belongs to the
class and not with instance of the class.
The static can be:
variable (also known as class variable)
method (also known as class method)
block
12

1) static variable
•The static variable can be used to refer the common property of all objects (that is not
unique for each object) e.g. company name of employees,college name of students etc.
•The static variable gets memory only once in class area at the time of class loading.

Advantage of static variable


 It makes program memory efficient (i.e it saves memory).
If you declare any variable as static, it is known static variable.
class Student{
int rollno;
String name; 13
static String college ="ITS";

Student(int r,String n){


rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+college);
}

public static void main(String args[]){


Student s1 = new Student (111,"Karan");
Student s2 = new Student (222,"Aryan");
s1.display();
s2.display();
}
}
14
Program of counter without static variable

class Counter{
int count=0;//will get memory when instance is created

Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){

Counter c1=new Counter();


Counter c2=new Counter();
Counter c3=new Counter();

}}
15
Program of counter by static variable

class Counter{
static int count=0;//will get memory only once and retain its value

Counter(){
count++;
System.out.println(count);
}

public static void main(String args[]){

Counter c1=new Counter();


Counter c2=new Counter();
Counter c3=new Counter();

}}
16

2) static method
If we apply static keyword with any method, it is known as static method
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
class Student{
int rollno; 17
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student.change();
Student s1 = new Student (111,"Karan");
Student s2 = new Student (222,"Aryan");
Student s3 = new Student (333,"Sonoo");
s1.display();
s2.display();
s3.display();
}
}
18

class Calculate{
static int cube(int x){
return x*x*x;
}

public static void main(String args[


]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
19

3)static block

Is used to initialize the static data member.


It is executed before main method at the time of classloading.

class A{
static
{
System.out.println("static block is invoked");
}

public static void main(String args[]){


System.out.println("Hello main");
}
}

You might also like