Posts

OCAJP | Interfaces

Image
Use Interfaces Declaring an interface  :   When you create an interface, that means you are defining a prototype of class, that what a class can do. In other words we can say interface is pre-declaration of class. Rules for defining an interface  : 1.  All methods of an interface are implicitly public & abstract whether you declare or not. 2.  All variables of interface must be public , static, or final i.e., we can declare only constants but not variables. 3.  Interface methods can’t be static. 4.  Because interface methods are abstract , so it can’t be final,strictfp or native. 5.  Interface can extend one or more other interfaces. 6.  Interface can’t extend anything other than interface. 7.  Interface can’t implement any class or other interface. 8.  Interface must be declared with the keyword interface. Some legal declaration of interface  : 1.  public abstract interface Boun...

Static Variable | Local Variable

Image
Topics Static Variable How to declare static variable When static variable will be created Class loading & Unloading Access static variable Local Variable When local variable will be created Conclusions STATIC VARIABLE If the value of a variable is not varied from object to object , then it is not recommended to declare variable as instance variable, we have to declare such type of variables at class level by using static modifier.   In the case of instance variable , for every object a separate  copy will be created, but in the case of static variables a single copy will be created at class level and shared by every object of the class. WHERE TO DECLARE STATIC VARIABLE : should be declared within the class directly , but outside of any block , method or constructor. WHEN STATIC VARIABLE WILL BE CREATED :   at the time of class loading. and will be destroyed at the time of class unloading. Hence, the scope of static variable is ...

Variables (Part-1)

Image
TYPES OF VARIABLES BASED ON TYPE OF VALUE REPRESENTED   P rimitive variable.   R eference variable BASED ON POSITION OF DECLARATION AND BEHAVIOR   Instance variable   Static variable   L ocal variable Base d  on type of value represented by a variable : Primitive Variables : used to represent primitive values: Ex int x = 10; Reference Vari ab les : used to refer object. Ex: Student s = new Student(); Based on position of declaration and behavior : Instance Variable : If the value of a variable is varied from object to object. Such type of variables are called instance variable For every object a separated copy of instance will be created. WHERE TO DECLARE INSTANCE VARIABLE: Instance variable should be declare within the class directly but outside of any method, block or constructor. WHEN INSTANCE VARIABLE WILL BE CREATED: At the time of object creation & destroyed at the time of object destruction. ...

Array Element Assignment

Image
Case 1:  In case of primitive types as array elements we can provide any type which can be implicitly promoted to declared type. ex:  int[] x = new int[4]; x[0] = 12; x[1] = 'a'; byte b = 13; x[2] = b; short s = 34; x[3] = s; x[4] = 16L;   // CE: Possible loss of precision found long required int  In case of float array the allowed data type are byte, short, char, int long float. Case 2:  In case of Object type arrays, As array elements we can provide either declare type objects or its child class objects. Ex: Object[] obj = new Object[10]; obj[0] = new Object(); obj[1] = new Integer(1); obj[2] = new String(" Arrays "); Ex: Number[] num = new Number[10]; num[0] = new Integer(10); num[1] = new Double(10.2); num[2] = new String(" Arrays ");  CE: Incompatible Types foud java.lang.String required java.lang.Number Case 3:  For interface type arrays as...