Q. What is method in java?
Ans. A Method is a group of statements which can be executed
to perform a specific task. Methods are used for the concept of reuse ability.
In Java programs at least one Method, which is main().
Q. What are the uses of Methods in Java?
Ans.
1) Java
Methods are used to avoid rewriting same logic/code again and again in a
program.
2) We can
call the Methods number of times.
3) With the help of Methods we can easily track the large program
of java
Example of simple program of Method
Write
a simple program for Method
class demo
{
void
dis()
{
System.out.println("Hello
Safal");
}
}
class function
{
public static void main(String args[])
{
demo s
= new demo();
s.dis();
}
}
OUTPUT
Hello Safal
Note:-You can save the file
in second class name e.g. function.java
Example 2: -
class boxmethod
{
int l,b,h,v;
void input()
{
l = 1;
b = 2;
h = 3;
}
void vol()
{
v = l*b*h;
System.out.println("vol is "+v);
}
public static void
main(String k[])
{
boxmethod bx = new boxmethod();
bx.input();
bx.vol();
System.out.println("vol is "+bx.v);
}
}
OUTPUT
Vol is 6
Vol is 6
Example 3: -
Learn More Examples and example of method with parameter
You like my post please share with friends. Any thing you do not understand please comment it will try to solve your problem
You like my post please share with friends. Any thing you do not understand please comment it will try to solve your problem
No comments:
Post a Comment