A method is something like a subprogram, which when invoked returns a value. In other words, a method is a collection of words or commands that joined to perform a function. Once this function or task is completed, it sends a result back to the caller.
It can even complete an operation without returning a value, and allows us to use the same code without actually having to type the entire code again and again. Methods are also known as instance members.
Syntax :-
[Modifiers] return_type Method_name ( Arguments1, array2---)
{
---
}
class can contain two types of methods:
package com.constructor;
public class StaticInstance {
int a= 10;
static int b=20; //static variable
void jbk() {
System.out.println("Hello JBK");
System.out.println(a);
System.out.println(b);
}
static void kiran()
{
System.out.println("Hello JBK");
System.out.println(b);
}
public static void main(String[] args){
//by creating object, both variables are printed
StaticInstance staticInstance=new StaticInstance();
System.out.println(staticInstance.a);
System.out.println(staticInstance.b);
}
}
Output:
10
20
Instance method | Static method |
---|---|
|
|
|
|
There are two types of variables, local and global. Read the following to get more details about them.
class A{
int a;
A a1;
A a2 = new A();
void m1()
{
System.out.println(a) ; //prints 0
System.out.println(a1) ; //prints null as every class is
// initialised with null
System.out.println(a2); //prints address of a2
}
}
Variables declared inside method or constructor or any block are called local variables
The scope of the local variable is within the method or constructor or block where it is defined
Memory will be allocated for the local variable whenever the enclosing block gets executed
Local variable is not equivalent to instance variable
Local variables cannot be static
Local variables cannot be referenced with class name or reference variable
Local variables will not be initialised by the JVM automatically, like instance and static variable
If you use local variables without initialising, you get compile time errors like ‘variable x might not have been initialised’.
Local variables can be final, primitive or reference
package com.javabykiran.Constructor;
public class AA {
static int ctr = 0;
int i = 100;
{ //This is block
System.out.println("Before change in local block");
System.out.println("ctr = " + ctr);
System.out.println("i = " + i);
int ctr = 2, i = 200;
System.out.println("");
System.out.println("After change in local block");
System.out.println("ctr = " + ctr);
System.out.println("i = " + i);
}
void display2() {
System.out.println("In another method");
System.out.println("ctr = " + ctr);
System.out.println("i = " + i);
}
public static void main(String args[]) {
AA sObj = new AA();
System.out.println(" ");
sObj.display2();
}
}
Output:
Before change in local block
ctr = 0
i = 100
After change in local block
ctr = 2
i = 200
In another method
ctr = 0
i = 100
package com.javabykiran.Constructor;
public class Hello {
static int a = 31;
static int b;
static void math(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialised.");
b = a * 4;
}
public static void main(String args[]) {
System.out.println("Before static method b = " + b);
math(42);
}
}
Output:
Static block initialised.
Before static method b = 124
x = 42
a = 31
b = 124