Search This Blog

Showing posts with label Interview Q. Show all posts
Showing posts with label Interview Q. Show all posts

Monday, March 14, 2011

this Keyword In Java

All java developer aware role of 'this'. 'this' is a reference to the current object in constructor and instance methods.

In java we use 'this' in different place are:

  1. Using 'this field' inside constructor/non-static method.

  2. Using 'this field' inside static method.

  3. Using this() method inside constructor

  4. Using this() method inside static/non-static methods.

  5. Recursive constructor invocation using this().


S.No.



Place
of 'this'



Description



When
executes



Where
Not Include



Sample
Code



1



Using
'this field' inside constructor/non-static method



'this'
act as the reference to the current object, So using 'this' we can
access the class instance variable and methods(static/no-static).


Most
of the case we use in POJO class setter & getter method.



We
can use to refer the current object instance variable inside the
constructor/non-static methods.



Its should
be used anywhere in inside constructor/non-static method.



package
thispck;


public
class
SampleThis


{ int
intvalue;


public
SampleThis(
int
intvalue)


{


this.intvalue=intvalue;


}


public
static
void
main(String arg[])


{


SampleThis
sampleThis=
new
SampleThis(10);


System.out.println("Int
Value::"
+sampleThis.intvalue);


}


}


Output:


Int
Value::10



2



Using
'this field' inside static method.



Not
possible to use this.



-



Compile
time error.


Error:


cannot use
this in a static context.



-



3



Using
'this() method' inside constructor



This()
method used for calling the default constructor and parameter
constructor in a class.






While
object creation using new keyword.



'this()'
declaration should be first statement in constructor.


Otherwise
compile time Error:


Constructor
call must be first statement in constructor






package
thispck;


public
class
SampleThis


{ int
intvalue;


public
SampleThis(
int
intvalue)


{


this.intvalue=intvalue;


}


public
SampleThis()


{


this(0);


}


public
static
void
main(String arg[])


{


SampleThis
sampleThis=
new
SampleThis(10);


System.out.println("Int
Value::"
+sampleThis.intvalue);


}


}


output:


Int
Value::10



4



Using
this() method inside static/non-static methods.



Compile
time error



-



Error:


Constructor
call must be the first statement in a constructor



-



5




Recursive constructor
invocation


using
this()



Compile
time error



-




Compile
time error.



public
SampleThis(int intvalue)



{



this();



this.intvalue=intvalue;



}







public
SampleThis()



{



this(0);



}



package
thispck;


public
class
SampleThis


{ int
intvalue;


public
SampleThis(
int
intvalue)


{


this();


this.intvalue=intvalue;


}


public
SampleThis()


{


this(0);


}


public
static
void
main(String arg[])


{


SampleThis
sampleThis=
new
SampleThis(10);


System.out.println("Int
Value::"
+sampleThis.intvalue);


}


}


Role of Super Keyword in Java

In this article trying to explain the use of 'Super' keyword in Child class.
  1. super() method @ Child class constructor

  2. Super() with parameter method @ Child class constructor

  3. Super()default/parameter method@ child class member method

  4. Super keyword @child class Constructor

  5. Super keyword @ child class member method

    Basically super keyword always refer the parent class object reference. We all know the parent of all java class is java.lang.Object. So when ever we create a java file(class) with out extending any class \,java compiler will do three basic operation.
  1. Extend parent class java.lang.Object

  2. if no constructor(default/Parameter) for the child class its include
    the default constructor
    .

  3. If no super() method in constructor its include the super() in the
    first line of the constructor.

    Fact:

when ever we create the object for any User define class we always able to
access the methods from the Object class.Its does means always
create two object in Heap memory one is user define class and another
one is Object class.



S.No



Place of
Super



Description



When
executes



Where Not
Include



Sample
Code


1


Super() default method @
Child class Constructer


Super()
its used for calling parent class constructer from child class
constructer.In this case two object will be created .


First
Parent object will be created and than child object will be
created.


While
creating object reference for the child class.


It should be Present in
first line of the child class constructer.


Should not be two super()
method statement in same constructer.


package
superpck.blog;


class
Parent{


public
Parent() {


System.out.println("Creating
Parent Object"
);


}


}


class
Child
extends
Parent


{


public
Child() {


System.out.println("Creating
Child Object"
);


}



}


public
class
SampleSuper {


public
static
void
main(String arg[])


{


Child
child=new
Child();


}


}


output:


Creating
Parent Object


Creating
Child Object



2


Super() with
parameter method @ child class.


Super()
with Parameter it used for calling parent class constructer with
parameter from child class constructer.In this case two object
will be created .


First
Parent object will be created and than child object will be
created.


But
we can initialize parent class instance variable from this.





while
creating object for the child class


Same as S.No:1(Super()
default method @ Child class Constructer)


package
superpck.blog;


class
Parent{


Integer
Parentage;


public
Parent() {


System.out.println("Creating
Parent Object"
);


}


public
Parent(
int
age){
Parentage=age;}


}


class
Child
extends
Parent


{


public
Child() {


System.out.println("Creating
Child Object"
);


}


public
Child(
int
Parentage) {


super(Parentage);


}



}


public
class
SampleSuper {


public
static
void
main(String arg[])


{


Child
child=
new
Child(30);


System.out.println("Parent
age::"
+child.Parentage);


}


}


output:


Parent
age::30






3



Super() default/parameter
method @ child class member method.



Error as :


Constructor
call must be the first statement in a constructor



-



Compile time error



-



4



Super keyword @ Child
class constrcter



Its act as a parent class
object reference from child class.Using super keyword we can call
the parenet class member methods and instance variables only not
constrcter.






Its
depends upon the Instruction flow.



It can present any where
and many time in both child class constrcter/


member methods.



package
superpck.blog;


class
Parent{


Integer
Parentage;


public
Parent() {


System.out.println("Creating
Parent Object"
);


}


public
Parent(
int
age){
Parentage=age;}


public
void
memberMethod()


{System.out.println("Inside
member method of Parent"
);


}


}


class
Child
extends
Parent


{


public
Child() {


System.out.println("Creating
Child Object"
);


}


public
Child(
int
Parentage) {


super(Parentage);


super.memberMethod();


super.memberMethod();


}


}


public
class
SampleSuper {


public
static
void
main(String arg[])


{


Child
child=
new
Child(30);


System.out.println("Parent
age::"
+child.Parentage);


}


}





Output:


Inside
member method of Parent


Inside
member method of Parent


Parent
age::30



5



Super keword @ child class
member method.



Its
act as a parent class object reference from child class.Using
super keyword we can call the parenet class member methods and
instance variables only not constrcter.



Its
depends upon the Instruction flow.



It can present any where
and many time in both child class constrcter/


member methods.



Refer
s.No 4 sample.


Friday, March 4, 2011

Static Keyword In Java

In this article i have explained the use of 'Static' keyword in different place of java.

like
1.Instance variable
2.In methods
3.In Class
4.In Inner Class -Create Object and call inner static and Non-Static methods.
5.Purpose of Static method and Static Class,static instance variable.
With Sample Program.

S.No

Place of static

Description

When executes

Where Not Include

Sample Code

1

Static block

Nothing but the block of code will be written inside of the static keyword in class .

Called only first time of object creation.

First priority will always go to static block even

normal block of code present

Inside Methods we can't declare a static block.

Package staticpck.block;

public class StaticBlock {

{ System.out.println("Inside empty block"); }

static {System.out.println("Inside static Block");}

}


package staticpck.block;

public class StaticBlockMain {

public static void main(String arg[])

{StaticBlock staticBlock=new StaticBlock();

StaticBlock staticBlock1=new StaticBlock();

}

}


Output:

Inside static Block

Inside empty block

Inside empty block


2

Static Instance Variable

Only one copy for the entire class.(each object reference have only one copy).

Even you can access static variable with out

creating reference for Object

Only one copy for entire class

(Only class name is enough to refer instance variable)

NA(Inside Method local static variable are not possible)

package staticpck.Instvar;


public class InstanceVar {

static int Instance=1;

}

package staticpck.Instvar;

public class InstanceVarMain {

public static void main(String arg[])

{ InstanceVar instanceVar=new InstanceVar();

instanceVar.Instance=2;

InstanceVar instanceVar2=new InstanceVar();

instanceVar2.Instance=3;

System.out.println("instanceVar.Instance="+instanceVar.Instance);

}}


Output:

instanceVar.Instance=3


3

Static @ method

Only one copy for the entire class.(each object reference have only one copy).

Even you can access static variable with out

creating reference for Object

When a method function is independent of its reference variable in that case we can use Static method.


package staticpck.method;

public class Method {

public static String getClassDesc()

{ return "This Class Refers Static Method";}

}


package staticpck.method;


public class MethodImp {

public static void main(String[] args) {

System.out.println("With Out Obj Ref Call::"+Method.getClassDesc());

System.out.println("With Obj Ref Call::"+new Method().getClassDesc());

}}


OutPut:

With Out Obj Ref Call::This Class Refers Static Method

With Obj Ref Call::This Class Refers Static Method

4

Static @ class

We can't use static for class.

-

Compile time error

-

5

Static in inside method

We can't use static inside method. No use to keep its in Stack memory

-

Compile time error

-

6

Inner class : static @ instance variable

Error msg:

The field “sample” cannot be declared static; static fields can only be declared in static or top

level types

-

Compile time error

To eliminate error declare inner class as static

7

Inner class : static @ method

Error msg:

The method “innermethod” cannot be declared static; static methods can only be declared in

a static or top level type

-

Compile time error

To eliminate error declare inner class as static

8

Static @Inner class

If u want to declare static instance/static methods inside the Inner class than Inner class must be declared as Static

All execution are same for outer class


package staticpck.inner;

public class InnerSample

{

static class InnerStatic{

static{

System.out.println("hi i am from inner static block");

}

{

System.out.println("Hi i am from Inner class block");

}

static int instance;

static String InnerMethod()

{ return "I am from Static InnerMethod"; } }

class Inner{

//static int instance;

String InnerMethod()

{

return "I am not able to use static due to My hirearchy";

}

}

}


package staticpck.inner;


public class InnerSampleImp {


public static void main(String arg[])

{

System.out.println(InnerSample.InnerStatic.InnerMethod());

InnerSample innerSample=new InnerSample();

InnerSample.Inner inner=innerSample.new Inner();

System.out.println();

}

}


Output:

hi i am from inner static block

Innser Static class:I am from Static InnerMethod