Pages

Sunday, 2 August 2020

How to call a method in Java? SDET Interview questions 2020












SDET Interview questions: How to access methods of a class in Java?

There are two ways to access method in class.

1. By creating object of class
2. Using static : If we use static then we can access the method without creating object of the class

Example of By creating object of class:

public class configReader {
     
  public int methodtwo(int a, int b)
// int is return type and public is access modifier. int a and int b are input parameters

{
      int sum = a+ b;
      return sum;
     
  }
 
  public static void main(String[] args) {
   
      configReader md = new configReader();  //Object Creation
      int result = md.methodtwo(2,3);               // access method by using object
      System.out.println(result);
  }
 
}

Example of accessing method with Static:

public class configReader {
 
  public static  int methodone(int a, int b) {
//static has been used so that with class name we can access the method
      int sum = a+ b;
      return sum;
  }
 
  public static void main(String[] args) {
      int resultone = methodone(3,4);            //accessing method directly without object
     System.out.println(resultone );
  }
}

If we want to access the same method in another class then use classname:
configReader.methodone(3,4)

For more Java related interview questions check below link:
*
**
**
*

*************************************************

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

No comments:

Post a Comment