Book Image

Hands-On Automation Testing with Java for Beginners

Book Image

Hands-On Automation Testing with Java for Beginners

Overview of this book

Java is one of the most commonly-used software languages by programmers and developers. Are you from a non-technical background and looking to master Java for your automation needs? Then Hands-On Automation Testing with Java for Beginners is for you. This book provides you with efficient techniques to effectively handle Java-related automation projects. You will learn how to handle strings and their functions in Java. As you make your way through the book, you will get to grips with classes and objects, along with their uses. In the concluding chapters, you will learn about the importance of inheritance and exceptions with practical examples. By the end of this book, you will have gained comprehensive knowledge of Java.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell

Using methods

Basically, methods are blocks in our Java class. Let's write one block here as an example, and observe where the opened and closed brackets are placed. The following example shows one complete block:

public void getData()
    static int a=4;

In this code, we have named the block of code getData() and void is the return type for this method.

If we are expecting to return a number from the method, and the number is an integer, then we have to write integer in place of void. The same applies with strings; if we are planning to return a string from the getData() method, then we have to declare it as a string. If we are not returning anything, that is, if we are simply writing a few lines of code, then we leave it as void.

Take a look at the following screenshot:

Return type is given as void for getData()

Here, we are not returning anything, so we keep it as void.

Let's add a return 2; line below System.out.println(" I am in method");. Here, we are returning a number that is an integer...