Top Posts
Virtual Function in C++
Ceil and Floor functions in C++
Python Rock Paper Scissors Game
AKTU MCA 4 SEMESTER QUESTION PAPERS
Git & Github 2021 Cheat Sheet
Introduction to C Programming Language
Git & Github 2021 Cheat Sheet
10+ C Program to Print Patterns
LUCKNOW BCA 6 SEM QUESTION PAPERS
Currency Converter in Python
TECHARGE
  • HOME
  • BLOGS
  • TUTORIALS
    • ALL TUTORIALS
    • PROGRAMMING TUTORIALS
      • JAVA TUTORIALS
      • C++ TUTORIAL
      • C PROGRAMMING TUTORIALS
      • PYTHON TUTORIAL
      • KNOWLEDGE MANAGEMENT TUTORIALS
      • DATA STRUCTURE AND ALGORITHM TUTORIALS
      • PROGRAMMING EXAMPLES
        • CPP EXAMPLES
        • JAVA EXAMPLES
        • C++ GRAPHICS PROGRAM
    • PROJECTS
      • PYTHON PROJECTS
      • SWIFT PROJECT
    • PPROGRAMMING QUIZ
    • DBMS TUTORIALS
    • COMPUTER NETWORK TUTORIALS
    • COMPUTER NETWORK SECURITY TUTORIALS
    • E COMMERCE TUTORIALS 
    • AWS TUTORIAL
    • INTERNET OF THINGS
    • CHEATSHEET
  • MORE
    • JOBS AND INTERNSHIPS
    • INTERVIEW PREPARATION
    • TECH BOOK
    • TECH NEWS
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • CONTACT US
  • WRITE +
  • ABOUT US
  • HIRE US
Java Tutorial

Methods in Java

by anupmaurya January 20, 2022
written by anupmaurya 0 comment 47 views

In this article you’ll learn about Methods in Java, Static Method, Static Block and The “finalize ()” method .

Methods

Table of Contents

  • Methods
  • Static Method
  • Static Block
  • The “finalize ()” method
  • A method is the block of code that can be called anywhere in a program. It contains a group of statements to perform an operation.
  • Combination of method name and parameter is known as method signature.
  • Required elements of method declaration are the method name, return type, a pair of parenthesis and body between braces {}.

Syntax

modifier returntype methodName (parameter)
{
     // body
}

Example

public static int display (int a, String b)
{
     //method body
}


where,
public static: modifiers
int: return type
display: method name
int a, String b: parameters
display (int a, String b): method signature

Example: Program for method

Program for swapping two numbers without using third variable.

class SwapNumber { public static void main(String[] args) { int n1 = 30; int n2 = 45; int temp; System.out.println("Before swapping, n1 = " + n1 + " and n2 = " + n2); swapMethod(n1, n2); } public static void swapMethod(int n1, int n2) { n1 = n1 + n2; n2 = n1 - n2; n1 = n1 - n2; System.out.println("After swapping, n1 = " + n1 + " and n2 = " + n2); } }
Code language: JavaScript (javascript)

Output:

Before swapping, n1 = 30 and n2 = 45
After swapping, n1 = 45  and n2 =30

Static Method

  • When a method is declared with static keyword, it is known as a static method.
  • A static method can be invoked without creating an object of the class.
  • Static method belongs to class level.

Static Block

  • Static block is also called as an initialization block.
  • It is executed only once before main method at the time of class loading.
  • A class can have any number of a static block.

Syntax:

class classname
{
     Static
     {
          //code          
     }
}

Example: Sample program for static method

public class StaticMethod { static { System.out.println("First static block"); //static block } static void m1 () { System.out.println("Static method"); } void m2() { System.out.println("Non-static method"); } Static { System.out.println("Second static block"); //static block } public static void main(String args[]) { m1(); //invoked static method StaticMethod sm = new StaticMethod(); sm.m2(); // invoked non static method } }
Code language: JavaScript (javascript)

Output

First static block
Second static block
Static method
Non-static method

The “finalize ()” method

  • Java runtime calls finalize () method when an object is about to get garbage collector.
  • It is called explicitly to identify the task to be performed.
  • The finalize() method is always declared as protected because it prevents access to the outside class.

Syntax

protected void finalize ()
{
     // finalize code
}
Share
0
FacebookTwitterLinkedinRedditWhatsappTelegramEmail
anupmaurya

Hey there, My name is Anup Maurya. I was born with love with programming and works at TCS. One of best global (IT) services and consulting company as System Administrator . I also love graphics designing. It's my pleasure to have you here.

previous post
Classes and Objects in Java
next post
Wrapper class in Java

You may also like

JDBC Components

Threads in Java

Life Cycle of a Servlet (Servlet Life Cycle)

XAMPP Tomcat/Error starting Tomcat

HttpServlet

GenericServlet

Inheritance and Access Modifiers in Java

Servlet Interface

Servlet API

Web Terminology

Java Tutorial

  • Online Java Compiler
  • Introduction to Java Programming
  • How to Check version of Java installed in System ?
  • How to set Temporary and Permanent Paths in Java
  • Features of Java Programming
  • Java Comments
  • Java Keywords
  • Java Variables
  • Data Types in Java
  • Java Flow Control Statements
  • Bitwise operators in Java
  • Understanding : public static void main(string args[])
  • Strings in Java
  • Final method in Java
  • Constructors in Java
  • Exception in JAVA
  • Exception Handling in Java
  • Arguments in Java with Examples
  • Difference between abstract class and interface
  • Interface in Java
  • Implementing Interfaces in Java
  • Java Event Handling
  • Extending Interface
  • Multithreading in Java
  • Threads in Java
  • Life cycle of a thread in Java
  • Java AWT tutorial for beginners
  • Introduction to JDBC
  • JDBC Components
  • Java Database Connections | JDBC Tutorial
  • DriverManager class
  • Connection interface
  • Statement interface
  • PreparedStatement interface
  • ResultSet interface
  • Java Database Connectivity with MySQL
  • Applet in Java
  • Difference Between Applet and Servlet in Java
  • Web Terminology
  • Servlets
  • Servlet Interface
  • Life Cycle of a Servlet (Servlet Life Cycle)
  • Servlet API
  • GenericServlet
  • HttpServlet
  • Steps to Create Servlet Application using tomcat server
  • Session Tracking Using Servlet
  • Introduction to JSP
  • The JSP API
  • The Lifecycle of a JSP
  • TAGS in JSP

Words from Readers

I Just go mad with this website I love the content and the way it you present I can easily understand anything from this Thank you everyone who are made this possible!

–Paras Singh Kaphalia

Recent Posts

  • Virtual Function in C++

    May 19, 2022
  • Ceil and Floor functions in C++

    May 19, 2022
  • Python Rock Paper Scissors Game

    May 18, 2022

EDUCATIONAL

  • PyScript: Python in the Browser

  • Best Fake Email Generators (Free Temporary Email Address)

  • How to Find Out Who Owns a Domain Name

  • Mobile phone brands by country of origin

  • Best way to use google search you won’t believe exist

  • 10 mostly asked questions related to WhatsApp

  • Top 8 Programming Languages That Will Rule in 2022

  • Google Cloud Platform

  • Best Online Code Editors For Web Developers

  • How to Write a Synopsis for Project Work

CHEATSHEET

  • Git and Github 2022 Cheat Sheet

  • ReactJs Cheatsheet

  • Linux Commands Cheat Sheet

  • C Programming language Cheatsheet

  • Scala Cheatsheet

  • MySQL Cheatsheet

  • Javascript Cheatsheet

PROJECTS

  • Python Rock Paper Scissors Game

  • Currency Converter in Python

  • Alarm clock GUI application with tkinter

  • Print emojis using python without any module

  • Country Date and Time using Python

  • Covid-19 Tracker Application Using Python

  • Python | GUI Calendar using Tkinter

  • Python: Shutdown Computer with Voice

  • Python GUI Calculator using Tkinter

  • Convert an Image to ASCII art using Python

  • Python YouTube Downloader with Pytube

  • Tic-Tac-Toe using Python

TUTORIALS

  • JAVA TUTORIAL
  • COMPUTER NETWORK
  • DBMS TUTORIAL
  • E-COMMERCE TUTORIAL
  • KNOWLEDGE MANAGEMENT
  • C++ PROGRAMMING
  • COMPUTER NETWORK SECURITY
  • AMAZON WEB SERVICES

TECH NEWS

  • PyScript: Python in the Browser

  • HalloApp is a secure alternative to WhatsApp, made by two early WhatsApp employees

  • 5+ Best Humanoid Robots In The World

  • Windows 11 Now Official, Brings Fresh UI, Centrally-Placed Start Menu

TERMS & POLICY

  • PRIVACY POLICY
  • TERMS AND CONDITIONS

COMPILERS

  • JAVA COMPILER
  • PYTHON COMPILER
  • JS COMPILER
  • C++ COMPILER
  • C COMPILER

JOBS AND INTERNSHIPS

  • TCS off-campus hiring 2022 for software engineers- 2019, 2020, & 2021 Batches

    February 27, 2022
  • Deloitte Recruitment For Any Graduates as Learning Operations Associate Analyst

    February 18, 2022
  • HP Recruitment For Tech Support Intern Position

    February 16, 2022
  • EY Hiring- PAS Global Immigration Advanced Analyst

    February 14, 2022
  • Amazon Recruitment Drive for Virtual Customer Support Associate Position

    February 12, 2022

@2019-21 - All Right Reserved. Designed and Developed by Techarge

TECHARGE
  • HOME
  • BLOGS
  • TUTORIALS
    • ALL TUTORIALS
    • PROGRAMMING TUTORIALS
      • JAVA TUTORIALS
      • C++ TUTORIAL
      • C PROGRAMMING TUTORIALS
      • PYTHON TUTORIAL
      • KNOWLEDGE MANAGEMENT TUTORIALS
      • DATA STRUCTURE AND ALGORITHM TUTORIALS
      • PROGRAMMING EXAMPLES
        • CPP EXAMPLES
        • JAVA EXAMPLES
        • C++ GRAPHICS PROGRAM
    • PROJECTS
      • PYTHON PROJECTS
      • SWIFT PROJECT
    • PPROGRAMMING QUIZ
    • DBMS TUTORIALS
    • COMPUTER NETWORK TUTORIALS
    • COMPUTER NETWORK SECURITY TUTORIALS
    • E COMMERCE TUTORIALS 
    • AWS TUTORIAL
    • INTERNET OF THINGS
    • CHEATSHEET
  • MORE
    • JOBS AND INTERNSHIPS
    • INTERVIEW PREPARATION
    • TECH BOOK
    • TECH NEWS
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • CONTACT US
  • WRITE +
  • ABOUT US
  • HIRE US