Monday, 16 March 2015

Q 13) Understanding Static ?




Static variable :

1.   The variable which is declared with a static keyword is known as static          variable.
2.     It will be initialize at class loading time.
3.     We can use the static variables in a different class without creating an            object reference.
4.     It can be called by its class name.
     Eg : classname.method() or classname.variable.
5.    We cannot use this keyword for static variable or static method



Q 14) Difference Between Static And Instance Variables ?




Static Variables
Instance Variables
01)
Static keyword is used in front of the variable.
Variables without static keywords are known as non static or Instance variables.
02)
JVM allocates memory at
 “ CLASS LOADING TIME”
JVM allocates memory of Instance variables at  
“ OBJECT CREATION TIME “
03)
Only one copy of static variables will be allocated for a number of objects in a class.
Number of memory blocks will be allocated with respect to number of objects.


Q 15) Static And Non Static Block/Method







Static Block
Non – Static Block
01)
JVM Executes the static block at
“CLASS LOADING TIME ”
JVM executes the non – static block at
“ OBJECT CREATION TIME ”
02)
Inside a static block we can only use a static variable directly but we cannot use a non static variable.
Inside a non static block we can use only non static variables.





Static Method
Non – Static Method
01)
Inside a static method we can only use a static variable directly.
Inside the non – static method we can use both static as well as non – static variables.
02)
3 ways to call Static methods
      With the class name
Eg: Test.disp1();
      Without object reference of a class
Eg: disp1();
      With object reference
Eg: t1.disp1();
1 way to call Non- static method
      With Object reference of the class
Eg : t1.disp();




Q 16 ) What Happens At Object Creation Time ?




Step 1 :  The JVM has to load the class, Since the class is not loaded yet.
Step 2 :  Memory will be allocated for reference variable ,and will be                        initialized to “ NULL ”.
Step 3 : Object will be created.
                 ( memory will be allocated to all instance variables and will be                     initialized to their default values depending on their
                    datatypes ).
Step 4 :  Instance block will be executed.
Step 5 :  Corresponding constructor will be called by the JVM.
Step 6 :  Object address will be assigned to the reference variables and

                 It becomes a complete “ OBJECT ”.

Q 17 ) What Happens At Class Loading Time ?




Step 1 : Byte code of the class will be placed in the main memory.
Step 2 : Default Constructor will be created.
Step 3 : Stack will be constructed for the class.
Step 4 : Memory will be allocated for the static variables.
Step 5 : A static block is executed.

( Static block will be executed first during the class loading time) 

Q 18) Difference Between this and super keywords




This :

·      It is used to resolve naming conflicts between local and instance                   variables.
·     We use this keyword to point instance variable of the current class.
·     This keyword is also called as reference variable which contains object         of the current class.
·     When the instance variable name is same, in that case we can refer this         keyword to differentiate form local and instance variables.


SUPER :

  •        Super keyword is used as a base class reference variable to refer the      base class instance variables and non static method.
  •    Super keyword is used to call the super class instance variable in            sub  class.

Q 19) Datatypes And Modifiers In Java ?




Datatypes :

There are 8 datatypes in java  :
               
Datatype
Value ( MIN & MAX )
Size
Default Value
1)
Byte
-128 to127
8 bits
0
2)
Short
-32,768 to 32,767
16 bits
0
3)
Int
- 2,147,483,648 to 2,147,483,647
32 bits
0
4)
Long
- 9,223,372,036,854,775,808  to
9,223,372,036,854,775,807
64 bits
0
5)
Float
Single precision
32 bits
0.0
6)
Double
Double Precision
64 bits
0.0
7)
Char
NA
16 bits
NULL
8)
Boolean
NA
1 bit
FALSE

  
Modifiers :

There are 12 Access Modifiers :

Access Modifiers
Access Modifiers
1)
Public
7)
Static
2)
Private
8)
Synchronized
3)
Default
9)
Native
4)
Protected
10)
Strict fp
5)
Final
11)
Transient
6)
Abstract
12)
Volatile

MODIFIER ACCESS :


Same Class
Same Package
Sub Class
Sub Package
Public
Y
Y
Y
Y
Protected
Y
Y
Y
X
Default
Y
Y
X
X
Private
Y
X
X
X