0% found this document useful (0 votes)
11 views

Java SE (Static Methods and Blocks)

Uploaded by

vatasem985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java SE (Static Methods and Blocks)

Uploaded by

vatasem985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

INTRODUCTION TO JAVA PROGRAMMING

LANGUAGE

(ELECTIVE-1)
LECTURE-18
Today’s Agenda

• Using “static” methods

• Factory methods

• “static” blocks
Using “static” Methods

• A method should be made static in following three situations


1. When it is only accessing static data of class
2. When it is only accessing its arguments and not using any data
members of the class.
3. When we have to create a factory method.

class MyMath
{
public static int max(int a, int b)
{
if(a>b)
return a;
else
return b;
}
}
class Test
{
Public static void main(String [ ] args)
{
int max=MyMath.max(10,20);
System.out.println(“Max is= ”+max);
or
System.out.println(“Maximum number is ”+MyMath.max(
10,20));
}
}
Factory Methods

• There are situations where creating an object might be dependent


on some conditions.

• For example, if someone enters 0 or negative age. In such


situations we have to check the conditions before the object can be
made.

• For such situations Factory methods can be used.

• Constructors of such classes are private and the factory method is


static in nature.

• Factory methods usually create and return Object. Let’s see an


example…
class Person static Person createPerson(int a,
{ String s)
private int age; {
private String name; if(a<=0)
private Person(int a, String s); return null;
{ else
age=a; {
name=s; Person P=new Person(a, s);
} return P;
public void show( ) }
{ }
S.O.P(age+“, ”+name); }
}
class Test
{
public static void main(String [ ] args)
{
Person p1, p2;
p1=Person.createPerson(-25,“Amit”);
p2=Person.createPerson(29,“Sumit”);
p1.show( );
p2.show( );
}
}
Properties of “static” method

• They are allowed to access only static data of the class


implicitly.

• They do not have “this reference” built in them.

• They cannot use the keyword super.

• They are called directly using class name, without using


object or object reference.

• All methods of Math class are static.


“static” Blocks

• Taking an programming situation

• Suppose we have to create an Banking application,


which shows accid, name and balance of a
customer. Along with this, application should
accept rate_of_interest at runtime and only once
and it should be common for all the customers.
How can we achieve this???

• The solution lies in using static blocks.

• Static blocks are independent blocks which are loaded in


RAM and executed as soon as the program executes.
Example

class Account
{
private int accid;
private String name;
private double balance;
private static double rate_of_int;
static
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter rate of interest”);
rate_of_int=sc.nextDouble();
}
Example

public Account()
{
Scanner sc=newScanner(System.in);
System.out.println(“Enter account id, name and balance”);
accid=kb.nextInt();
name=next();
balance=nextDouble();
}
public void show()
{
System.out.println(name+”\n”+accid+”\n”+balance);
}
public static void showRate()
{
System.out.println(“Rate of interest is “+roi);
}
}
Example

class Test
{
public static void main(String [] args)
{
Account A1= new Account();
Account A2= new Account();
Account A3= new Account();
A1.show();
A2.show();
A3.show();
Account.showRate();
}
}
End Of Lecture 18

You might also like