Coding Fun Java Selenium Class 15 – Java Methods – User Defined Methods

Selenium Class 15 – Java Methods – User Defined Methods





Core Java or Java Standard Edition Elements for Automated Testing, Java Programming for Selenium, Methods in Java, Java Predefined Methods and User defined Methods.
Java Static and Non Static Methods, Java Methods with return a value and Java methods without return any value. Create Object in Java and Call Non Static Methods using the Object. Arguments usage in Java methods and Advantages of Java Methods.

Tags: , , , , , , , , , , , , , , , ,

4 thoughts on “Selenium Class 15 – Java Methods – User Defined Methods”

  1. Thanks Nice Explanation

  2. G C Reddy says:

    Class Notes:
    Selenium Class 15: Java Methods – User Defined Methods
    —————————————————————–
    i) Introduction to Java Methods
    ii) Types of Methods
    iii) User defined Methods
    ————————————————————-
    i) Introduction to Java Methods

    What is Method?

    > Java Method is a set of Statements that are grouped together to perform an
    operation.

    > Methods are equivalent to Functions in Structured Programming

    > In Structured Programming (Ex: C Language) we use Functions (Predefined and User defined)

    > In Object Oriented Programming (Ex: Java Language) we use Methods (Predefined and User defined)
    ————————————
    When we choose Methods?

    Whenever we want to perform any operation multiple times then we choose Methods

    Advantages of Methods

    Code Reusability…
    Code Maintenance is easy
    —————————————————————————-
    Types of Methods in Java

    1) Predefined Methods
    2) User defined Methods

    1) Predefined Methods

    Java has a library of classes and methods organized in packages

    import java.io.Console;

    import java.io.*;

    > In order to use predefined libraries we need to import packages or classes
    Note: Some features are Built-in, ex: java.lang package is automatically imported in every Java Program

    > Using "import" keyword we can import predefined libraries
    ——————————————————————–
    Categories of Predefined Methods

    1) String Methods
    2) Array Methods
    3) Number Methods
    4) Character Methods Etc…
    —————————————————————–
    iii) User defined Methods
    1) Method with return a Value
    a) Call Methods by invoking Object (Non Static Methods)
    b) Call Methods without invoking Object (Static Methods)

    2) Method without return any value
    a) Call Methods by invoking Object (Non Static Methods)
    b) Call Methods without invoking Object (Static Methods)
    ————————————————————
    Note:
    i) Write/Create Methods outside of main method but within the class
    ii) Call Methods within the main Method

    public class JavaMethods { //Class Open
    //Outside of the main Method(Create Methods)
    public static void main(String[] args) {//main Method Starts
    //Within the main Method (Call Methods)
    }//main Method ends
    //Outside of the main Method (Create Methods)
    }//Class Close
    ——————————————————————————
    User defined Methods Examples
    1) Method with return a Value
    a) Call Methods by invoking Object (Non Static Methods)

    Syntax for Creating Methods:

    accessModifier returnType methodName (Parameters…){
    Statements
    ———–
    ————–
    ————-
    return Statement
    }

    Note: methods take parameters, but parameters are optional, if you don't want to use any parameters
    then write (),

    () //Blank – No Parameters
    (int a, int b) -With parameters
    (String name)
    (int a, int b, int c)

    Note: Use Parameters based on your method return type only, don't use different data type of parameters

    Calling Methods:

    //Create Object
    ClassName methodName = new ClassName();

    Call:
    dataType variableName = objectName.method(values for parameters);
    ————————————————————————-
    Example:
    public class JavaMethods {

    public static void main(String[] args) {
    //Create Object
    JavaMethods obj= new JavaMethods();

    //Call Methods
    int x=obj.add1(123, 234);
    System.out.println(x);

    int y=obj.add2();
    System.out.println(y);

    //Or
    System.out.println(obj.add1(12, 23));//35
    System.out.println(obj.add2());//60
    }
    //Create a Non static Method with return a value and parameters…
    public int add1(int num1, int num2){
    int result=num1+num2;
    return result;
    }
    //Create a Non static method with return a value and without parameters
    public int add2(){
    int a=10, b=20, c=30;
    int res=a+b+c;
    return res;
    }
    }
    —————————————————————————–
    1) Method with return a Value
    b) Call Methods without invoking Object (Static Methods)

    Syntax for Creating Methods…

    accessModifier static (nonAccessModifier) returnType methodName (Parameters){
    Method Body
    .
    .
    .
    return statement
    }

    Calling Static Methods

    dataType variableName = ClassName.methodName(values for parameters);
    //Or
    dataType variableName = methodName(values for parameters);
    ———————————————————————–
    Example:

    public class JavaMethods {
    //Create a Static Method with return a value and parameters
    public static int multiply1(int a, int b){
    int result=a * b;
    return result;
    }
    //Create a Static Method with return a value and No Parameters
    public static int multiply2(){
    int x=10, y=30;
    return x*y;
    }
    public static void main(String[] args) {
    //Call Static methods

    //Method returning value assign to a variable
    int res1=JavaMethods.multiply1(12, 34);
    System.out.println(res1);

    res1=multiply1(2, 4);
    System.out.println(res1);//8

    //Display the Method return value directly
    System.out.println(JavaMethods.multiply1(3, 4));//12
    System.out.println(multiply1(4, 5));//20
    }
    }
    —————————————————————————-
    2) Method without return any value
    a) Call Methods by invoking Object (Non Static Methods)

    Syntax for Creating Methods

    accessModifier void returnsNothing methodName (Parameters){
    method body
    .
    .
    }

    Example:
    public class JavaMethods {
    //Create a Non Static Method and returns nothing
    public void comparison(int a, int b){

    if (a>b) {
    System.out.println("A is a Big Number");
    }
    else {
    System.out.println("B is a Big Number");
    }
    }
    public static void main(String[] args) {
    //Create Object
    JavaMethods abc = new JavaMethods();

    abc.comparison(100, 20);

    }
    }
    ————————————————————-
    2) Method without return any value
    b) Call Methods without invoking Object (Static Methods)

    Syntax:

    accessModifier static (nonAccessModifier) void (returnsNothing) methodName (Parameters){
    Method Body
    .
    .
    }
    ————————————————————————-
    Example:
    public class JavaMethods {
    //Create a Static Method and returns nothing
    public static void comparison(int a, int b){
    if (a>b){
    System.out.println("A is a Big Number");
    }
    else {
    System.out.println("B is a Big Number");
    }
    }
    public static void main(String[] args) {
    JavaMethods.comparison(12, 34); // B is a Big Number
    comparison (100, 50);//A is a Big Number
    }
    }
    ————————————————————-
    Usage of Methods
    1) Internal Use (Create and Call Methods within the Same Class)
    2) External Use (Calling Methods from another Class)
    ————————————————————————-
    //Internal Use of a Method

    public class Class1 {
    //Create a Non Static Method
    public int add(int a, int b){
    int result = a+b;
    return result;
    }
    public static void main(String[] args) {
    //Create Object
    Class1 obj= new Class1();
    int res=obj.add(123, 456);
    System.out.println(res);
    }
    }
    ————————————————————————
    //External use of a Method

    Class 1:
    public class Class1 {
    int i=100;
    //Create a Non Static Method
    public int add(int a, int b){
    int result = a+b;
    return result;
    }public static void main(String[] args) {
    }
    }
    ——————-
    Class 2:
    public class Class2 {

    public static void main(String[] args) {
    //Call a Method from an another Class within the same package
    Class1 obj = new Class1();
    int x =obj.add(13, 23);
    System.out.println(x);//36
    System.out.println(obj.i);//100
    }
    }
    ———————————————————————————————
    Accessing Class Members from one class to another within the same package

    Class 1:

    > Create Object using Parent Class Name and then you can access Class members from the Parent Class
    public class Class1 {
    public static int add(int a, int b){
    int result = a+b;
    return result;
    }public static void main(String[] args) {
    System.out.println(Class1.add(12, 34));//46

    System.out.println(add(10, 20));//30
    }
    }
    ———————————————————–
    Class 2:
    public class Class2 {

    public static void main(String[] args) {
    //Call a Static Method form an another Class in the same package
    System.out.println(Class1.add(10, 20)); //30
    }
    }
    ———————————————————–

Leave a Reply

Your email address will not be published. Required fields are marked *