Methods in Java

A class represent the properties and behabiour of an object. An methods represent these behaviours.

Methods in Java are blocks of code with

  • a name

  • a paranthesis that can accept zero or more parameters

  • can optionally return a value.

  • has a body block enclosed within two curly braces {}.


Signature and body of a method

The first line of a method with its access specifier (who all can access), return type (what will this method return to its caller),  name and the paranthesis with zero or more parameters, is called the method signature.

The body follows the signature starting with { and ending with }.

Example: The main method

When you execute a Java class (using java command) in console, JVM first calls a predefined method called main with a particular signature and then we can start calling other methods from there.

public static void main(String[] args)


//method body


Explanation of various terms used:

  • public - is the access specifier that tells how visible the method is within and outside the class. Possible values are public, private, protected and default (no access modifier). 

  • static - this is a static method, not an instance method.

  • void - return type. void means nothing is returned.

  • main - name of the method.

  • args - A String array to accept command line arguments.


Rules for declaring a method

  • It is mandatory to specify return type while declaring a method; if nothing is returned, specify it as void. 

  • We should return a value compatible with the return type and do any casting if required. 

  • If you have specified the return type as void you can either omit the return statement or simply have the return statement as return;. 

  • We can have any primitive type like int, float etc or any reference type (class or interface) as the return type.   

  • Like variables, methods can also be static or instance. Static methods will have a static keyword in its signature line (as in the case of main method).

  • From a static context (e.g. static method) you cannot access non-static members (e.g. non-static variables or methods). However, from non-static context (e.g. non-static method), you can access static members (e.g. static variables or methods).



Invoking methods

  • Methods are not automatically invoked like initialization blocks and constructors, but need to be invoked explicitely from within the code; only exception is the main method which is invoked automatically by the JVM.

  • Invocation is similar to the usage of instance and static variables, but need to pass any required arguments. 

    • For instance if your method signature says add(int a, int b), you need to invoke it as add(1,2) or add(x,y) where x and y are integers.


Using object reference for instance methods

Instance methods are invoked using an object reference.

Example: An instance method with signature public void myMethod(int i) can be invoked as:

MyClass mc = new MyClass();



Saving the return value after calling a method

If the method return a type, we can assign it to a variable of the same type. An instance method with signature public void myMethod() can be invoked and return type assigned to a variable as:

MyClass mc = new MyClass();

int i = mc.myMethod();


Invoking static methods

Like static variables, static methods can be invoked either using the object reference or the class name. 

MyClass mc = new MyClass();



It is always preferred to use class names with static variables and methods.


Method Overloading

We can have multiple methods with same name, but different signatures. 

public static void main(String[] args){}

public static void main({}

  • We can overload even the main method, but Java will only call the one with predefined signature when a class is invoked. We can however invoke the other from within the main method.

  • Return type is no part of signature and hence two methods with same name and argument types, but different return types are not valid overloading which is not allowed.

  • Correct version of the method will be invoked based on the types you specify while invoking. Hence whenever you see a declaration and/or invocation, make sure you can invoke without confusion as Java will not allow any declaration or invocation which can lead to ambiguity.


Blocks within methods

When a block without any name appear inside a method body block, the block is considered as a single statement and executed when the containing method is executed.

A block within the main method will look as:

public static void main(String[] args) //outer block

  int var1=0;

  int var2;

  //inner block within method


    int var3=0;





  /* Below line won't compile as var2 is not initialized.  */


  /* Below line won't compile as var3 is not visible outside the block in which it was defined.  */







Sir, when we declare a method private , it means that we can't access those from outside the class until we call them from a public method.But the main use of it is the user shouldn't be able to see the implementation of the method and change it . Knowing this fact , we cannot change the logic of normal methods too while calling them . So what's the exact use of it sir ? can you explain in breif sir ?

Was it useful?

private in java is just to ensure proper encapsulation (object oriented princple). it deoes not have anything to do with security. this is a common misconception among many.

Your code should follw the design principle of "Open Close Principle", which means your code should be open to changes and closed to modifications. When changes comes there should be minimal or no modification.

you should only expose few APIs that are necessary. this will allow less changes to client code. public methods will be used by others and hence changing it will require all those people who use your methods to change their code. This is against the Open Close Principle.

Please read through notes on object oriented programming and design principles, for better understanding.

Was it useful?

I have referred that but still any small practical example sir ?

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)