➧Methods in Java - Introduction :
We also call "Methods" as "Functions" in Java. Many times when we are developing a big software So we do not have to write any code or any information repeatedly, so we all need Methods.
The code written inside the Methods can be implemented by the Programmer, where it wants to implement, only the class in which the method is made, has to make an object of that class and call its method. By calling that method, we can do its property wherever we want to implement it. Using the method, code written inside it does not have to be repeated anywhere.
"Methods" is a block inside which Programmer writes some Statements. These statements are easily used by calling their methods where necessary. Using Methods reduces the size of our program and becomes in Program Short. Time also gets reduce.
For "example" we can take the println ("...") function. The println () function is a Pre-defined Function that is placed in the java library in the "java.io" package, in the PrintStream class. Whenever we have to print any statements, we call it Simple bit println ("...") and use it. println ("...") would have been defined somewhere within a class anywhere in Java's Library, so it is Responsible to print a Statement.
Source Code for Pre-defined Methods :
OUTPUT: Shivam
Syntax :
Access_specifier :
In the Java Programming Language, Access_specifier defines how the method is accessible in any other class. Such as Public, Private, Protected If public, then everyone will use its property. Private will not be able to use any of its properties and if it is protected it will be able to access both of it itself and its sub-class only.
Return_type :
Return type means that our method will return the type of value. Such as int, float, string and if "void" is typed it means that our method will not return any value.
In Method_name we write the name of our method, whatever we have to type the name of our method, this name can be anything. It depends on the programmers.
Parameters :
Parameters are passed here and Parameters are also passed here. It depends on the programmers. Because Methods are used in two types, one is Parameterized Methods and the second Non-Parameterized Methods.
For ex :
Syntax for Method Calling :
1. In same class -> Method_name( args1,args2,….);
2. In another class -> Object_name.Method_name(args1,args2,….)
Example for main Method :
Read Also :
Methods with No Arguments :
While making these types of methods, some arguments can also be passed in it. This argument holds the Value given at the time of calling this method by creating an object of the class of this method. For example, let's understand this through a Java Source Code -
Source Code :
OUTPUT : a + b = 30
Methods with Arguments :
Source Code :
OUTPUT : a + b = 30
Static Methods :
To create Static Method we use "static" keyword in that method. By using the "static" keyword in the method, we can call that method without having the object of the class of that method, for example we understand the multiplication of two numbers with the Java Source Code.
Source Code :
OUTPUT : 200
Thank You !
Note - For More Core Java & Advance Java Tutorials, Concepts & Programs Are Available in www.kptechsolution.com
Also Read :
Like this post, If you find Topic useful in this post, so share it with your friends and colleagues on Facebook, Twitter, whatsapp and Google Plus.
Thank You !
Note - For More Core Java & Advance Java Tutorials, Concepts & Programs Are Available in www.kptechsolution.com
0 Comments