Methods, Variable and Block
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.
Methods
Syntax :-
[Modifiers] return_type Method_name ( Arguments1, array2---)
{
---
}
class can contain two types of methods:
- Instance methods
- Static method
- When you define a method inside a class without a static keyword, it is called the instance method or the non-static method
- When you define a method with static keyword then it is called the static method
- Instance methods must be called by
you explicitly. You can call using reference variables which contain the object.
For example:(Hello h=new Hello()) - Static methods must be called by you explicitly. You can call using the following methods:
- With classname
- With reference variable without initialisation. For example:
A a=null;. - With reference variable which contains object
Example:
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 vs Static Method
| Instance method | Static method |
|---|---|
Variables
There are two types of variables, local and global. Read the following to get more details about them.
Global Variables:
- Global variables are initialised automatically
- It is defined outside of method
- A static keyword can be applied to global variable
- Its scope is across class and can be used anywhere
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
}
}
Local Variables:
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
Local Block
- Block defined inside a method or block or constructor is called a local block
- A local block will be executed whenever the enclosing methods or constructor or block is executed
- Local blocks are not equivalent to instance block
- Local blocks cannot be static
- You can write local blocks inside a method or constructor or block, and it can be nested
Example
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