Professional Documents
Culture Documents
State Member: Program: Test
State Member: Program: Test
{ res: 0
{
new Sample().show(); // it calls-non-static Function Member of the Sample
int res = new Sample().v1 % 10;
System.out.println("res: " + res);
Sample.test(); //call-static-function-member of Sample class
}
}
// static-members-present-in-different class
class Demo
{
static int a = 10; // static-DATA-MEMBER
static int b = 20; // static-DATA-MEMBER
static int sum = 0; // static-DATA-MEMBER
public static void addition() // static-FUNCTION-MEMBER
{
sum = a + b;
System.out.println("Addition : " + sum);
}}
class StaticMember1
{
public static void main(String[] args) OUTPUT:
{
Addition : 30
Demo.addition(); // call-static-function-member
a : 10
System.out.println("a : " +Demo.a);
opVar : 30
int opVar = Demo.a + 20;
System.out.println("opVar : " + opVar);
}
}
2. Static Member Present in Same class
Program:
class StaticMember2
{
static int a = 10;
static int b = 20;
static int sum = 0;
public static void addition()
{
sum = a + b;
System.out.println("Addition : " + sum);
}
public static void main(String[] args) OUTPUT:
{ Addition : 30
addition(); a : 10
System.out.println("a : " + a); opVar : 20
int opVar = a + 10;
System.out.println("opVar : " + opVar);
}
}
Non-static-Members: Program
1. Non static member Present in Different class
Program:
// non-static-members-present-in-different-class
class Addition
{
int a = 10; // non-static-data-member
int b = 30;
int sum = 0;
public void addition() // non-static-function-member
{
sum = a + b;
System.out.println("Addition of " + a + " and " + b + " is: " + sum);
}
} OUTPUT:
class NonStaticMember Addition of 10 and 30 is: 40
{ opVar : 20
public static void main(String[] args)
{
new Addition().addition(); // create object of Addition class
and access the function member
int opVar = new Addition().a + 10;
System.out.println("opVar : " +opVar);
}
}
2. Non static member Present in Same class
Program:
// non-static-members-present-in-same-class
class NonStaticMember2
{
int a = 30; // non-static-data-member
int b = 20;
int sub = 0;
int sum = 0;
public void addition() // non-static-function-member
{
sum = a + b;
System.out.println("Addition : " + sum);
}
public void subtraction() // non-static-function-member
{ OUTPUT:
Addition : 50
addition();
Subtraction : 10
sub = a - b;
System.out.println("Subtraction : " + sub);
}
a1.checkBal();
a1.deposit(1000);
a1.checkBal();
a3.withdraw(500);
a1.checkBal();
a2.checkBal();
a2.deposit(5000);
a2.checkBal();
a2.withdraw(1000);
a2.checkBal();
a2.withdraw(50000);;}}
Global and Local Variable: “this” keyword
Program:
// Global and Local Variable
class Test
{
static int a = 10; // global-static-variable
double b = 23.12; // global-non-static-variable
public static void display() // static-method
{
int a = 30; // local-variable
System.out.println("this is display() of Test class");
System.out.println("local variable - a : " + a); // accessing local variable
System.out.println("global-static-PV - a : " + Test.a); // accessing-global
-static-variable
}
public void view() // non-static-method
{
int b = 2312; // local-variable
System.out.println("\nthis is view() of Test class");
System.out.println("local variable - b : " + b); // accessing-local-variable
System.out.println("global-non-static-PV - b : " + this.b); // accessing
-global-non-static-var
}
}
class GlobalVariable
{
public static void main(String[] args)
{
Test ref = new Test(); // Object of Test class
ref.display(); // using Ref.var accessing the methods of Test class
ref.view();
/* or
Test.display(); // it calls static-FM of Test class
new Test(); // it calls non-static-FM of Test class
*/
} OUTPUT:
} this is display() of Test class
local variable - a : 30
global-static-PV - a : 10
// static-block
class Blocks
{
static int a = 10;
static // static-block
{
System.out.println("this is static-block 1 of Blocks class");
}
static
{
System.out.println("this is static-block 2 of Blocks class");
}
public static void display() // static-method
{
System.out.println("a : " + a);
}
} OUTPUT:
class StaticBlock this is static-block 1 of Blocks class
{ this is static-block 2 of Blocks class
public static void main(String[] args) a : 10
{ a : 10
new Blocks();
new Blocks();
Blocks.display();
Blocks.display();
}
}
2. Non-static Block: Program
// non-static-block
class Blocks
{
static int a = 20;
{ // non-static-block
System.out.println("this is non-static-block 1 of Blocks class");
}
{
System.out.println("this is non-static-block 2 of Blocks class");
}
} a : 20
Constructors:
1 Zero-Argument Constructor: Program
class Constructor1
{
public Constructor1() // Zero-Argument-Constructor
{
System.out.println("this is Constructor1() constuctor of Constructor1 class");
}
public void display() // normal-method
{
System.out.println("this is display() of Constructor1 class");
}
}
class ZeroArgConstructor
{
public static void main(String[] args)
{
Constructor1 refVar = new Constructor1(); //
refVar.display(); OUTPUT:
new Constructor1();
this is Constructor1() constuctor of Constructor1 class
}}
this is display() of Constructor1 class
this is Constructor1() constuctor of Constructor1 class
Parameterized Constructor: Program
// parameterized-constructor
class Constructor2
{
int sum = 0;
public Constructor2(int a,int b) // parameterized constructor
{
sum = a + b;
System.out.println("Addition : " + sum);
}
public void display() // normal method
{
System.out.println("this is display() of Constructor2 class");
}
}
class ParameterizedConstr
{
public static void main(String[] args)
{
Constructor2 refVar = new Constructor2(20, 40);
refVar.display();
new Constructor2(10,20);
}
OUTPUT:
}
Addition : 60
this is display() of Constructor2 class
Addition : 30