05 Java Fundamental Method Overloading, Static
05 Java Fundamental Method Overloading, Static
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.
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.
class Counter{
int count=0;//will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){
}}
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);
}
}}
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;
}
3)static block
class A{
static
{
System.out.println("static block is invoked");
}