Methods | Java: Methods are behavior of class.
Syntax: class class_name {
void/int method_name(par1,par2,...,parn)
{
// statements...
}
}
Here is the Code:
- import java.io.*;
- class Main {
- public static void main (String[] args) {
- A a = new A();
- a.setX(3);
- System.out.println(a.getX());
- }
- }
- class A
- {
- int x;
- void setX(int x)
- {
- this.x = x;
- }
- int getX()
- {
- return this.x;
- }
- }
Output:
3
Here is the Video: