Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SECTION 02:

Object Creation: Program


class Sample
{
int v1 = 100; // data-member
public static void test() //function member
{
System.out.println("This is test() of Sample class");
}
public void show()
{
System.out.println("this is show() of Sample class");
test();
OUTPUT:
System.out.println("v1: " + v1);
} this is show() of Sample class

} This is test() of Sample class

class ObjectCreation v1: 100

{ res: 0

public static void main(String[] args) This is test() of Sample class

{
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
}
}

State Member: Program

1. Static Members Present in Different class


Program:

// 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);
}

public static void main(String[] args)


{
new NonStaticMember2().subtraction();
}
}
Reference Variable: Program
class Account
OUTPUT:
{
long accNo = 987654321; Address of the 'a1' object:
String owner = "someone";
String branchName = "BENGALURU"; Section2.Account@43a25848
static String bankName = "ICICI"; Address of the 'a3' object:
double bal = 0.0;
Section2.Account@43a25848
public void checkBal() Address of the 'a2' object:
{
System.out.println("\nAccount Bal:" + bal); Section2.Account@1e643faf
}
public void deposit(double amt)
{ Account Bal:0.0
if(amt > 0) Account Bal:1000.0
{
bal = bal + amt; Account Bal:500.0
} Account Bal:0.0
else
{ Account Bal:5000.0
System.out.println("invalid amount"); Account Bal:4000.0
}
} insufficient balance
public void withdraw(double amt)
{
if(amt <= bal)
{
bal = bal - amt;
}
else
{
System.out.println("insufficient balance");
}
}
}
class ReferenceVariable
{
public static void main(String[] args)
{
Account a1; //reference variable
a1 = new Account();
Account a3 = a1;

Account a2 = new Account(); // create new object and initialized address


of the object to the reference variable 'a2'

System.out.println("Address of the 'a1' object: " + a1);


System.out.println("Address of the 'a3' object: " + a3);
System.out.println("Address of the 'a2' object: " + a2);

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

this is view() of Test class


local variable - b : 2312
global-non-static-PV - b : 23.12
BLOCKS

1. Static Block: Program

// 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");
}

public static void display()


{
System.out.println("a : " + a);
}
}
class NonStaticBlock
{
public static void main(String[] args)
{
OUTPUT:
new Blocks();
Blocks.display(); this is non-static-block 1 of Blocks class

new Blocks(); this is non-static-block 2 of Blocks class

} a : 20

} this is non-static-block 1 of Blocks class


this is non-static-block 2 of Blocks class

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

You might also like