Method:
Depends upon the data behavior we have two methods.
1. Static :
------------
When do we go for static methods?
case 1: Whenever the business implementations are not going to change or constant then we will go for constant
case 2: Whenever we want to create read-only services
case 3: Without creating an object we want to access services
2. Non-static:
-----------------
when do we go for non-static methods?
Whenever the implementations are not constant then we will go for non-static.
How to define a static method:
Syntax:
<access specifier> static void methodname(--------)
{
------------------
}
static <access specifier> void methodname(--------)
{
------------------
}
<access specifier> static <return type> methodname(--------)
{
------------------
return <stmt>;
}
static <access specifier> <return type> methodname(--------)
{
------------------
return <stmt>;
}
Access specifier is not mandatory, it's optional
Specifying access of a class
If we want to put some restrictions on our business method
Static:
static is a keyword to convey it as a static method
Void:
it returns nothing
whenever we are keeping the method void, the business implementations are going to be executed but in the end, it won't return anything
Whenever you are keeping return type after executing the business implementations it's going to return some values.
Method name rules:
every method must and should have some name
it should start with a small letter
it should not start with numbers or special characters
method name should be clear and very meaningful
if a method name has multiple words every first word should start with a capital letter except the first word
Ex: addition_Program
As a part of the return type what should allow?
void is allowed
primitives like int, short, byte, long, float, double, boolean, char,
class type as string allowed
interface type also allowed
<access specifier> static <return type> methodname(--------)
{
------------------
return <stmt>; /* is mandatory
}
return <stmt>; =======>
whenever our method is having a return type then the return statement is mandatory
we should not write anything after the return statement.
the return type is always based on return statement
return type should be same types as return statement of integer or higher than integer
if we are trying to return lower than integer then we will get compile time error
return statement should be the last statement inside the method otherwise we will get compile time error.
After the return statement, we should not take any statement otherwise we will get a compile-time error.
if we want to return some values then go for return type
if return statement is returning some type then the return type should be same or higher than that type.
but not lower otherwise we will get compile time error.
For the business method, we should take only one return statement we should not take more than one otherwise we will get a compile-time error.
Real-time approach:
ATM:
BR: Cash deposit:
----------------------
savings balance=25000
insert the atm card
valid card or not
if it is invalid===> invalid card
card is valid
enter the account number
insert the amount
it goint to save the amount
amount is deposited
25000+10000=35000
savings balance=35000
After the cash is deposited we want to return the savings balance
case1: if you want to return the savings balance at the end of the business implementations then go for <return type>.
case2: if you don't want to return the savings balance then go for <void type>.
For example take a calculator and perform 25+20 then it will return 45 value.
loginTestDemo() ===> valid
for more readability differentiate with _
login_Test_Demo() ===> more readable
class Demo{
int num1=25;
int num2=65;
or
int num1=25, num2=65;
public static void additionProgram() /* it wont return anything because void
{
int result=num1+num2;
}
}
if we want to see the result or return something then
class Demo{
int num1=25, num2=65;
public static int additionProgram() /* it wont return anything because of void
{
int result=num1+num2;
return result;
}
}
Lower to higher:
byte ===> short ===> int ===> long ===> float ===> double
char
int can hold char long float double
int cannot hold byte short
No comments:
Post a Comment