Skip to main content

Java Methods

 Java Methods

What is Java Method?

  • A method is a block of code that performs a specific task  
  • A method in Java is a set of instructions that can be called for execution using the method name 
  • A Java method can take in data or parameters and return a value - both parameters and return values are optional. 
  • Methods can be public, private or protected. Method is a set of statements to perform an operation,methods are also known as procedures or functions
  • A Java function is a block of code designed to perform a specific task, encapsulated within a class or interface

Naming a Method

  • In Java language method name is typically a single word that should be a verb in lowercase or a multi-word, that begins with a verb in lowercase followed by an adjective, noun. After the first word, the first letter of each word should be capitalized.

Rules to Name a Method:

  1. While defining a method, remember that the method name must be a verb and start with a lowercase letter.
  2. If the method name has more than two words, the first name must be a verb followed by an adjective or noun.
  3. In the multi-word method name, the first letter of each word must be in uppercase except the first word. For example, findSum, computeMax, setX, and getX.

Why are methods used in Java?

It allows code reusability (define once and use multiple times) You can break a complex program into smaller chunks of code

1. Code Reusability: Methods allow you to write a block of code once and reuse it multiple times throughout your program. This saves time and effort, and makes your code more efficient.
2. Modularity: Methods help break down complex programs into smaller, manageable parts. This improves code readability and makes it easier to understand, debug, and maintain

Syntax of the methods in Java?



Method Signature: Every method has a method signature. It is a part of the method declaration. It includes the method name and parameter list.
 
Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method does not return anything, we use void keyword.

Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its name.

Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.

Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces


Types of Methods in Java

1. Predefined Method
 predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method
2. User-defined Method
The method written by the user or programmer is known as a user-defined method.  

   1.Instance Method :  Access the instance data using the object name. Declared inside a class
   2.Static Method : Access the static data using class name. Declared inside class with static keyword
   3.Abstract Method : abstract methods mean "you have to implement it later point of time" type method ..


Comments

Popular posts from this blog

Step-by-Step: Launch Browser, Context, and Page in Playwright and Run Test and Configuration (JavaScript)

πŸŽ₯ Setup Browser, Context, Page & Run Config Test Scripts with package.json & playwright.config.js Step-by-Step: Launch Browser, Context, and Page in Playwright and Run Test and Configuration (JavaScript) 1. Install Playwright You can install Playwright using the following command: npm init playwright@latest 2. Create a Basic Test Script Understand the core Playwright architecture: Element Description browser Controls the browser instance (like Chrome, Firefox, etc.) context Acts like a separate browser profile (cookies, localStorage are isolated) page A single browser tab where interaction happens 3. Run the Test npx playwright test example.spec.js Ways to Run TypeScript Tests Way Command Notes 🟒 Via npx npx playwright test Uses built-in TypeScript support 🟒 With s...

Playwright Test Structure in Details -Session-02

πŸŽ₯ Playwright: test.only, Hooks & Grouping with test.describe Explained Let’s go step-by-step , showing how to build from a single test , to using beforeEach / afterEach , and then organizing things with test.describe . ✅ Step 1: Basic Single Test with test.only import { test, expect } from '@playwright/test' ; test. only ( 'πŸš€ Basic test - check title' , async ({ page }) => { await page. goto ( 'https://example.com' ); await expect (page). toHaveTitle ( /Example Domain/ ); }); test.only ensures only this test runs — great for debugging. ✅ Step 2: Add beforeEach and afterEach import { test, expect } from '@playwright/test' ; test. beforeEach ( async ({ page }) => { console . log ( 'πŸ”„ Setting up before each test' ); await page. goto ( 'https://example.com' ); }); test. afterEach ( async ({ page }, testInfo) => { console . log ( `πŸ“¦ Finished test: ${testInfo.title} `); }); test. only ( ...

Playwright Locators in JavaScript (Complete Guide)

🎯 Playwright Locators in JavaScript (Complete Guide) This guide explains each Playwright locator with: ✅ What it is πŸ• When to use ⚙️ How to use it 🎯 Benefits πŸ§ͺ Code Examples πŸ”Ή 1. Locator by ID ✅ What: Selects an element with a unique id . πŸ• When: Element has a unique id . ⚙️ How: page.locator('#username') 🎯 Benefit: Fast and reliable. <input id="username" /> await page.locator('#username').fill('John'); πŸ”Ή 2. Locator by Class ✅ What: Selects by class . πŸ• When: Repeated or styled elements. ⚙️ How: page.locator('.password') 🎯 Benefit: Useful for shared styling. <input class="password" /> await page.locator('.password').fill('12345'); πŸ”Ή 3. Locator by Text ✅ What: Matches visible element text. πŸ• When: For buttons, links, etc. ⚙️ How: page.getByText('Login') 🎯 Benefit: Human-readable. <button>Login...