Wednesday, June 3, 2015
this keyword
public class ThisKeyordDemo {
public static void main(String[] args) {
ClassName cNam = new ClassName();
cNam.methodName();
System.out.println("Reference className is "+cNam);
System.out.println("\n------------Another Object----------");
ClassName cNam2 = new ClassName();
cNam2.methodName();
System.out.println("Reference className is "+cNam2);
}
}
class ClassName{
int num = 10 ;
public void methodName(){
int num = 15;
System.out.println("This will print local variable num: "+num);
System.out.println("This will print the instance variable num "+this.num);
System.out.println("\nReference className is "+this);
}
}
-------------------------------------------------------------------------------------------------------------------------
public class ThisConstructors {
public static void main(String[] args) {
ClasName cName = new ClasName();
ClasName cName2 = new ClasName(1);
ClasName cName3 = new ClasName(2,6);
ClasName cName4 = new ClasName(3,7,true);
ClasName cName5 = new ClasName(4,8,true);
System.out.println("First obj "+cName);
System.out.println("Second obj "+cName2);
System.out.println("Third obj "+cName3);
System.out.println("Fourth obj "+cName4);
System.out.println("Fifth obj "+cName5);
}
}
class ClasName{
int x,y;
boolean b;
char c;
public ClasName() {
this(0, 0, false, ' ' );
}
public ClasName(int x) {
this(x, 0, false, ' ');
}
public ClasName(int x , int y) {
this(x, y, false, ' ');
}
public ClasName(int x , int y ,boolean b) {
this(x, y, b, ' ');
}
public ClasName(int x , int y , boolean b , char c) {
this.x = x;
this.y = y;
this.b = b;
this.c = c;
}
}
Implicit - Explicit Parameters
Implicit Parameters :
// The object on which the methods are called are called IMPLICIT parameters
// The Datatype of the implicit parameters is the class in which the methods are defined
public class ImplicitParameters
{
public static void main(String[] args)
{
Names myData = new Names();
myData.myName = "Bpn";
myData.printMe(); // IMPLICIT parameters
}
}
class Names
{
String myName;
public void printMe()
{
System.out.println(myName);
}
}
Explicit Parameters :
// Any parameters that are supplied to the methods within its brackets becomes explicit parameters
// here int length = Explicit parameters
public class ExplicitParameters
{
public static void main(String[] args)
{
MyData myData = new MyData();
int area = myData.getArea(20);
System.out.println(area);
}
}
class MyData
{
public int getArea(int length) // EXPLICIT Parameters
{
return length * length;
}
}
// The object on which the methods are called are called IMPLICIT parameters
// The Datatype of the implicit parameters is the class in which the methods are defined
public class ImplicitParameters
{
public static void main(String[] args)
{
Names myData = new Names();
myData.myName = "Bpn";
myData.printMe(); // IMPLICIT parameters
}
}
class Names
{
String myName;
public void printMe()
{
System.out.println(myName);
}
}
Explicit Parameters :
// Any parameters that are supplied to the methods within its brackets becomes explicit parameters
// here int length = Explicit parameters
public class ExplicitParameters
{
public static void main(String[] args)
{
MyData myData = new MyData();
int area = myData.getArea(20);
System.out.println(area);
}
}
class MyData
{
public int getArea(int length) // EXPLICIT Parameters
{
return length * length;
}
}
Tuesday, June 2, 2015
Object Reference Preservation
CLICK TO WATCH VIDEO
public class ObjRefPreservationDemo
{
public static void main(String[] args)
{
B b = new B();
b.x = 10;
System.out.println("Before method call b.x is "+b.x);
A a = new A();
a.methodA(b);
System.out.println("After method call b.x is "+b.x);
}
}
class B
{
int x ;
}
class A
{
public void methodA(B obj)
{
System.out.println("inside method call before modifying obj.x is "+obj.x);
obj.x = 300 ;
System.out.println("inside method call after modifying obj.x is "+obj.x);
obj = null;
System.out.println("After putting null obj is "+ obj);
}
}
PrimitiveType VS ObjectRefer..
Primitive types :
Primitive Types VS Object References :
public class PrimitiveTypesVSobjectReferenceMemoAllocationDemo {
public static void main(String[] args) {
System.out.println("CASE OF PRIMITIVE TYPES");
int x = 5 ;
int y = x;
System.out.println(x + " and "+ y +" before");
x = 10;
System.out.println(x + " and "+ y +" after");
y = 20;
System.out.println(x + " and "+ y +" before");
System.out.println("\nCASE OF OBJECT REFERENCES TYPES");
Test a = new Test();
Test b = a;
System.out.println(a.name + " and "+ b.name+ " before");
a.change();
System.out.println(a.name + " and "+ b.name+ " after");
b.change();
System.out.println(a.name + " and "+ b.name+ " after");
}
}
class Test{
String name = "binni";
public void change() {
name = "Bpn";
}
}
Primitive Types VS Object References :
public class PrimitiveTypesVSobjectReferenceMemoAllocationDemo {
public static void main(String[] args) {
System.out.println("CASE OF PRIMITIVE TYPES");
int x = 5 ;
int y = x;
System.out.println(x + " and "+ y +" before");
x = 10;
System.out.println(x + " and "+ y +" after");
y = 20;
System.out.println(x + " and "+ y +" before");
System.out.println("\nCASE OF OBJECT REFERENCES TYPES");
Test a = new Test();
Test b = a;
System.out.println(a.name + " and "+ b.name+ " before");
a.change();
System.out.println(a.name + " and "+ b.name+ " after");
b.change();
System.out.println(a.name + " and "+ b.name+ " after");
}
}
class Test{
String name = "binni";
public void change() {
name = "Bpn";
}
}
Scope Of Variables
// Instance variables alive as long as the object alive
// Iorder to initialize the instance variable we can use constructors
// Instance variables have the default values which is NULL
// But local variables do not have default values so we have to declare it.
// local variable alive aslong as the method alive
// parameter variable are declared in the method header which is also the local variables
// Iorder to initialize the instance variable we can use constructors
// Instance variables have the default values which is NULL
// But local variables do not have default values so we have to declare it.
// local variable alive aslong as the method alive
// parameter variable are declared in the method header which is also the local variables
Subscribe to:
Posts (Atom)












