According to OOPS ( Object-oriented concept ) inheritance are classified into 6 category in which java support 5 of them and one is not supported in java i.e multiple inheritance for which there is a concept of interface . Interface is used to make JAVA support for multiple inheritance. The following 6 types of inheritance are as follows :-
1. Simple Inheritance
2. Multiple level inheritance ( Not supported in Java )
3. Multi-level inheritance
4. Hierarchical Inheritance
5. Hybrid inheritance
6. Cyclic inheritance ( Not supported in Java )

Single Inheritance
Single inheritance is a simple hierarchy of super and subclass. Here a subclass is extending its super class and super class is by default extending its super class i.e Object class. So if there is a single class the object class is the direct super class otherwise it will be the indirect super class. 

Simple Inheritance

 Multiple Inheritance 
In this type of inheritance we have more than one super class and only one subclass. and we are inheriting all the super class member in the sub-class and to achieve we need to extend all of these classes but unfortunately Java doesn't allow to extend more than one super class. This is why it is not supporting multiple inheritance but to achieve the same there is new concept of interface. 

Multiple Inheritance


Multi-level inheritance 
In multilevel inheritance we are following series of super and sub class relation but it is in a single level that means a subclass is extending only one super class but that super class is again extending another class. Like this it follows the chain of extending class one to another in a single level. So in this a class sometimes acts as a sub for its super class and sometimes acts as super class for its sub-class. 

Multi-Level Inheritance


Hierarchical Inheritance 
It is type of inheritance where we have one super class and more than one subclass. Here every subclass extending super class will able to access the member of its super class and note there is no inheritance relation between sub classes. Like if we have to use the member of the class into all other classes then hierarchical inheritance would be the best option. 

Hierarchical Inheritance


Hybrid inheritance 
It is a type of inheritance which is the combination of simple, multi-level and hierarchical inheritance. So by using hybrid we are going to use all the functionality of all other types of inheritance.

Hybrid inheritance


Cyclic inheritance 

It is a type of inheritance in which a class extends it self and form a loop itself. Now think if a class extends it self or in any way if it forms cycle within the user defined classes then is there any chance of extending Object class ?
No then there is no chance of extending super class and hence not supported by Java. 


Cyclic inheritance
Cyclic inheritance 2
 





It is a concept in java which provide the re usability of the members of one class/object to another class/object. Inheritance term itself defines the property inheriting. In java we are working with class so here we are going to inherit the property of one class into another i.e we are reusing the unresticted members of one class into other class who is inheriting that class. Now before going into depth you need to understand two term
1> Super class / parent class / inherited class.
2> Sub class / child class / inheriting class.

Super class

In java super class is the parents class of all the class who is inheriting that class. By default Object class of lang package is the super class of all class. By inheriting the super class we are free to use all the unrestricted member into subclass for our own custom implementation. Like by extending predefined classes we are using predefined methods .

Sub class

Subclass is the child class and is inheriting the members of its super class. For inheriting any class we need to use the extends keyword so that JVM will understand that a class is being inherited by another class.
The class which is going to use extend keyword is known as sub-class. For ex-

class A { }                                             
public class B extends A { } 

Here in the above program class B is extending class A that means
B --> Subclass
A --> super class 
and by default A is extending Object class that means A is subclass for Object class. So here is the Hierarchy for the above program how it is inheriting each other.



So now according to inheriting classes we can categorize inheritance into six category

1.  Simple Inheritance
2.  Multiple Inheritance  ( Not supported In java )
3.  Multi-level Inheritance
4.  Hierarchical Inheritance
5.  Hybrid Inheritance
6.  Cyclic Inheritance

For details on the above types of inheritance follow click here !




  1. Introduction to Java
  2. Setting PATH and CLASSPATH in java and configure it in Window 7
  3. Simple program to start with coding in java 
  4. Some important points keep in mind while writing any java program
  5. How and Why to use main() method in java?
  6. Compilation and Interpretation in java
  7. Importance of " import " statement in java
  8. Data Types in java
  9. Literals in Java
  10. Operators in Java   
  11Type Casting in Java
   13. Array In Java
   14.Static Keyword in java
   15How to define constant in java?
   16Final method in Java
   17Object in Java
   18Method overloading in java
   19VAR-ARGS ( Variable Arguments ) in Java
   20Command line argument
   21Variables in Java
   22Class loading in Java
   23Some Important points regarding writing any Java program
   24Inheritance in Java 
   27Super keyword in Java
 
 
28Inner Class 
  29Anonymous class in java
  30Access modifiers in java
 31Accessibility of access modifiers in java
 32. Abstract keyword in java
 33. Interface in java
 34. Dynamic dispatch in java
1> Save any java program with .java extension only.

2> In a source file we can have as many import statement but is should be 2nd statement if there is any package statement.

3> Any package statement must be the first statement otherwise compiler will give compilation error.

4> Class file will be generated for any many class defined in a single source file .
For ex- a program has 3 classes then compiler will create 3 .class file

5> Its not mandatory to have main method in a class. But if we have any class with main() then it will compiler successfully but JVM fails to execute the .class file because of no main() definition.

6> If we try to execute any class without main() then JVM will give "NoSuchMethodError ".

7> For any java source file main() is the starting point of executing the program for the JVM.

8> We cannot have more than one public class in a single java program.

9> If there is a public class then its mandatory to save the file with the name of the class followed by .java extension but can execute any class.

10> If there is no public class in our program then we can save it with any class name and can able to execute any class but the class must have main() other JVM will give " NoSuchMethodError ".

11> One program can have more than one main () but each class can have only one main().

12> If we have more than one class and in every class we have defined a main() then the only main() from the specified class will be executed.

13> One class can have only one class / interface definition and can have any number of non-public class definition.

14> Its not mandatory to have main() in public class only. We can have main() in non-public class as well but the execution of main() depend upon the specified main() while executing the program.



Class loading is a process which is done by the JVM after compilation. When we are compiling then the task of compiler is to check the syntax error which is called as compilation and hence after successful compilation a .class file is created which is a compiled file and now before processing that class, it is first loaded by the JVM and while loading .class file  JVM will allocate the memory for the static variable and this is why for any static member only once the memory is created and it will remain throughout the program and can be accessed by any object. 

After initializing all the static member now JVM will allocate memory for the object (if user write valid syntax for object creation in the program). If user don't write any code for object creation then JVM is not going to allocate any memory for any Object defined in the program.

Steps followed while class loading by JVM.

1> While class loading JVM will instantiate all the static member.

2> It will try to execute any static block available in the program.

3> It will initialize all the instance member through the default constructor by creating the object.

4> It will execute any instance block available in the program but the instance block is never going to initialize the instance member.

Lets see and example to clear out all the above points.

public class prog {

int x ;                                 // instance variable
static int y ;                         // static variable

static{                                // static block
System.out.println(z);
}

{                                    // instance block
System.out.println(x);
}

public static void main(String sush[]){
prog   p = new prog ();             // object creation
   }
}

In the above program we can see that
1> X is a instance variable that means it must be initialized while creating the object.
2> Y is a static variable that means it will initialized while loading the class by the JVM.
3> There is a static block execute after initializing the static variable.
4> Instance block is being executed by the JVM.

Diagram representing task of JVM while class loading


Note : While class loading JVM will keep all the information about the static variable, static block , static method, instance variable, instance block, instance method, constructor etc in the memory location of the main memory called as method area.
Can you imagine if you have an app and sometimes you can't access the code due to some problem( may be path problem ) but you have the .apk file with you, then you need some of the changes in the code for running purpose as the client is waiting.
Then what you will do?
                         Think Think Think..
GOT???
                   NO
Here is the solution for this,

1) First download the apktool from https://code.google.com/p/android-apktool/downloads/list according to OS you are using.

2)Then download SignApk.zip from http://www.mediafire.com/download/gs6n10oh09c039n/SignApk.zip.

3) Extract it and save it in particular folder.For Example it is.
            E:\applications\apktool
So now in   E:\applications\apktool there is 3 ( 2 apktool (1 for all and another for your OS),1 signapk file ) types of file.Extract it.

4) Put the apk file which you want to edit in the apktool folder.

5) Take the apktool.jar  and paste it in that folder.

6) Open cmd(Command prompt) and go to that apktool location where .apk file is available.
     Then type      apktool d [Your apk file].apk

7) And be sure don't close your CMD ( Command prompt ).

8)Now a folder with the name of Your apk file will be availble. Edit what you want.
   Now its the Time for gain compile the app.

9)Type the following to compile your app -
          apktool b [Your old apk name][Your new apk name].apk

10) Must not close your CMD.

11) Open the apktool folder in explorer. In that folder, you will now see a new file called [desired name of new apk file].apk. Then copy this file to SignApk folder.

12)Now to sign the apk. If you don't sign it, you will be unable to install it. Go to the command prompt, and type:
         java -jar signapk.jar  [name of apk you just copied].apk [another name].apk

                                Now the apk has changed and signed also..




We all know that Android is one of the most popular mobile platform. But how many people know that Android Versions are named in alphabetical order. Lets reveal the secret of its version which follow the alphabetical order.
These are the android version names :
1.Cupcake
2. Donut
3. Eclair
4. Froyo
5. Gingerbread
6. Honeycomb
7. Ice Cream Sandwich
8. Jelly Bean















9. Guess what is its next version ?
The Upcoming next version is : Kitkat (Android 4.4)
         







The above logo is the upcoming version of the android i.e Kitkat where you will find some interesting new features and something which was not in its previous version. 
Transaction is the process of performing multiple database operations as one unit with all nothing criteria.
I:e when all the database operations in the unit are successful then transaction is successful and should be committed. When any one database operation in the unit are failed then transaction are failed and should be rolled back.

When you implement transaction property in your application it guarantees ACID properties.
A- Atomicity
C- Consistency
I- Isolation
D- Durability

Atomicity : It will give you a guarantee that the whole work will be performed as a unit. If one part of transaction fails then entire transaction fails.

Consistency : Consistency is about ensuring that any transaction will bring the database from one valid state to another.

Isolation : Many transaction may run concurrently. These concurrently running multiple transaction may disturb other transaction that is multiple transaction should run isolately.

Case A : Consider the case where multiple transaction running concurrently and using multiple rows of account table.
tx1 -> 101
tx2 -> 102
tx3 -> 103

1. withdraw from 101
2. withdraw from 102
3. withdraw from 103

Case B : Consider the case where multiple transaction running concurrently and using single account row of account table.
acc no: 99

1. transfer(withdraw)
2. bank teller(withdraw(deposite))
3. loan EMI(withdraw)
in case B you may get some problem which is called as transnational concurrency problems.

  1. Dirty read problem.
  2. Repetable read problem.
  3. Phantom read problem. 
To avoid these problems, you have to apply one of the following required transactional isolation levels.

  1. READ_UNCOMMITTED
  2. READ_COMMITTED
  3. REPETABLE_READ
  4. SERIALIZABLE
Durability : Yoyr enterprise data should be available for long time as long as your enterprise is running. You have to protect your data from crashes, failures, you have to implements your proper backup and recovery mechanism and proper loging mechanism.

Dirty read problem :

  • When transaction reads the dirty value(modified but not committed) then you may get some inconsistent result.
  • To avoid dirty read you have to lock the column(cell).
  • To lock the column you have to apply isolation level called READ-COMMITTED.
Repetable read problem :

  • When a transaction is reading the same row repeatedly, you may get different set of values in different reads. this kind of problem is called repeatable read problem.
  • To avoid repeatable read problem you have to lock the row.
  • To lock the row you have to apply isolation level called REPEATABLE-READ.
Phantom read problem :

  • When a transaction is reading the set of row repeatedly you may get different set of rows in different reads this kind of problem is called phantom read problem. 
  • To avoid phantom read you have to lock the entire table.
  • To lock the table you have to apply isolation level called SERIALIZABLE.
Types Of Transaction :

  1. Local transaction :
    When a single database is participating in the transactional operation then it is called local transaction.
  2. Distributed transaction :
    When two or more database are participating in the transaction operations then it is called as distributed transaction.
Types Of Transaction :

  1. Flat transactions : A simple transaction that perform one or more operations as a unit operation.
  2. Nested Transactions : as the word nested says one transaction under another transaction.



Configuration or AnnotationConfiguration :

  1. These are available in org.hibernate.cfg package. This is the first class that will be instantiated by hibernate application.
  2. You can use Configuration or AnnotationConfiguration class object for 2 task :
    -> calling configure or configure(String) method.
    -> calling buildSessionFactory() method.
  3. configure() method is responsible for :
    -> reading the data from hibernate configuration document.
    -> reading the data from all the hibernate mapping documents specified in configuration document.
  4. buildSessionFactory() is responsible for creating SessionFactory object.
  5. Configuration or annotation configuration object is single threaded and short lived.
  6. Once SessionFactory object is created then there is no use with Configuration or AnnotationConfiguration object.
SessionFactory :
  1. This interface is available in org.hibernate package.
  2. SessionFactory object is Multi-Threadded and long lived.
  3. When you call buildSessioFactory() method on Configuration object then following task will happen.
    -> set the default to many parameters like batch, size fetch size etc.
    -> generate and catches the sql queries required.
    -> select the Connection provider
    -> select the TransactionFactory.
  4. SessionFactory is :
    -> factory of Session object.
    -> client for connection provider.
    -> client for TransactionFactory.
  5. You need to create one SessionFactory per database.
  6. when you are using multiple databases then you need to write multiple hibernate configuration docs.
Session : 
  1. This is an interface available in org.hibernate package.
  2. Session object is single threaded and short lived.
  3. Session represents period of time where user can do multiple database operation.
  4. Session Object :
    -> uses TransactionFactory to get the transaction.
    -> get the connection from ConnectionProvider.
Transaction :
  1. When transaction is started the following task will happen.
    -> session cache will be created
    -> connection will be taken and will be associated with the current session.
  2. While transaction is running following task will happen.
    -> when any object is participated in session operations, that will be placed in session cache.
  3. When transaction is committed the following task will happen :
    -> session will be flushed(here the synchronization of persistent data with the database will be performed)
    -> session cache will be destroyed
    -> commit will be issued to database
    -> connection will be released


You will become suprised to heard that a small 18 years old girl who wants to be a designer,get success in her life by designing the great Android Logo.

Irina Blok was a designer  at Google now famous for her great design:-The Green Android Robot Logo. For Designing the Logo,the inspiration came from bathroom door.

The outline of men and women have become   universally recognized,so she wants to design for android.
For making it opensource,She made it green..
Beneath is the logo of the android which looks like a robot :-


                                                      
Now you can imagine how near by things around all of us can make a person inspired to develop a better and advanced platform for mobile which is now using by millions of people through out the world. So gain something by thinking differently !
We can categorize the types of variables in java as 
1> Local variable 
2> Static variable
3> Instance variable
4> Reference variable 

Static and instance variable can be of reference type.And both static and instance are the scope of class it means we are defining in the class only. We cannot define static variable in the local scope like in the method block. For details about static variable Click Here . 

Local variable
Local variables are having local scope to access or we can say that they are having temporary access. A variable defined in the method or looping block are considered as a local variable. Memory for the local variable are created while invoking the method and destroyed when execution of the method block ends that means every time memory for the local variable will be creating when JVM will invoke instance / static method and will be destroyed when it finishes method block execution. If are we are defining any variable inside the looping block then its scope will remain within the looping block only. And every time memory will created and destroyed while executing and finish executing the block respectively. 

Static variable
1> Static variable is the scope of class that means it is instantiated by the JVM while loading the class. 
2> Most importantly static variable can be accessed without creating the object. 
3> We can be able to access the static variable either with the class name or directly. 
4> For static variable memory will be allocated once and will be while loading the .class file.
5> Any change in the memory location of the static content, will reflect to all the accessing object. 
6> Use of static variable is when we need to assign the same content for all the objects.
7> Making use of static variable is good practice as because memory allocated once by the JVM.
8> Static variable is accessible any where in the class.

For details about Static keyword in java then just  Click Here . 

Instance variable
Like static variable it is also the scope of the class but it is only accessible with the object only and constructor is responsible for allocating the memory for the instance variable and object is responsible for the calling constructor.

By default its value is zero if variable is of primitive type and null if the it is of reference type. 

Memory created for the instance variable is different for different object. It means if we are modifying the instance variable with one of the object then will not reflect to the other object. 

We cannot access instance variable from the static content. But why ?
It is because static content can be accessible without the creation of any object but to access the instance member we need an object. That is why compiler is not very sure that developer is surely going to create for in its program and hence it will give compilation error "non-static variable x cannot be referenced from a static context" .


Possible ways to access the instance variable 

1> With the reference that has the object reference value. For ex-


< class_name >  < var_name >  = new < class_name >( ) ;

< var_name > . < instance_variable > ;

2> With the object directly . For ex - 

new  < class_name > . <  instance_variable > ; 

Reference variable
Reference variable can be both static and non-static. It is generally used to store the reference value of the object and hence points the object memory location. 

Assigning null value to any reference means that now it is not assigning to any object in the heap memory.

What will happen when we will define any reference as static ?
Defining static to a variable is used for making singleton class. Singleton class means restricting user to create more than one object.  


Let me make you clear with an example regarding variables

public class prog {

int x ;                        // instance variable
String y ;                    // instance reference variable  
static int i ;               // static primitive variable 
static String j ;           // static reference variable


public static void main(String sush[ ]) {

prog p = new prog() ;       //Object creation 
prog p1=null ;             //Reference variable with null value
   }
}
Command line argument is the facility in java to pass the string value while executing the class file after the compilation.While executing the class file we can give as many string value separated with the space. These values are passed to the main method with array of String as an argument and the size of the array will be decided as per the value provided while executing. JVM will invoke the main () with array of string and can able to print by using the array index individually or with the help of for loop. We can also print the length of the array.

Example of the command line argument

public class prog {

public static void main(String sush[ ]) {
System.out.println( sush [0] ) ;

   }
}

Compiler -- javac prog.java

Execute---- java prog  javatutorial

Here we are executing our java program by passing javatutorial as a string value, which get stored in the first index of our array of string defined in the main() i.e sush[ ] .Like this we can be able to pass as many string value as we want. If you want to print any integer value then simply pass internally it will be converted into string.

Note : Size of the array depend on the number of value you are passing. If you are passing one value and trying to print more then one index then it will run-time exception as "ArrayIndexOutOfBoundsExecption ".For ex -

public class prog {

public static void main(String sush[ ]) {
System.out.println( sush [0] ) ;
System.out.println( sush [1] ) ;    // Run-time exception

   }
}

Compiler -- javac prog.java

Execute---- java prog  javatutorial

While executing the above statement JVM will throw run-time exception as we are passing only single value any trying of print more than the size of the array.

All the above program are valid with any version of JDK but java vendor has introduced a new feature from java 5 of variable length argument. So in the main method we can also VAR_ARGS as a parameter.Internally it is converted into array by the compiler.It is represented with three dots. For more about VAR-ARGS Click Here ! .For ex-

public class prog {

public static void main(String... sush) {
System.out.println( sush [0] ) ;

   }
}

Compiler -- javac prog.java

Execute---- java prog  javatutorial

So keep experimenting with the codes in java !
It is a new feature introduced from java 5. It is same as array but in case of variable arguments we are passing directly value were as in case of array we need to create manually an array object. The task we are doing of creating an array is done by compiler itself in case of VAR-ARGS.

Important points regrading VAR-ARGS

1> Its simply reduced the task of creating an array manually, compiler will internally create the array of the specified type , only we need to pass the direct value in case of parameters passing.

2> The above we can do with the help of array as well but we need to create an array object and pass the array reference to the overloaded method or method call.

3> In case of array we cannot provide the actual value directly always, we need to provide the array object. But in case of VAR-ARGS we can directly pass value, compiler will internally convert it into an array.

4> To define VAR-ARGS we need to use three dots ( . . . ) after the data type name followed by the variable name.

5> VAR-ARGS will be converted as an array object internally by the compiler itself , to verify you can decompiler the .class file with the help of any java decompiler.

6> VAR-ARGS can be used as an array to access the data and we can use both length property and index notation to access the member declared as Variable argument.

7> While invoking the method with parameter as an VAR-ARGS we can pass either the direct value or an array reference pointing an array object of specified type.

8> In the same program we cannot have overloaded method with parameter as an array object and VAR-ARGS object . For ex-

class prog {

void show ( int[ ]  arr ){ }   // int type array  
void show ( int...  arr ) { }  // int type VAR-ARGS 
}

In the above code , for the compiler both are having same parameter i.e of array so it create and ambiguity for the compiler that which method is to be overloaded.

9> In case of overloading if we are using VAR-ARG as a parameter then it should be the last argument declared in the overloaded method. For ex-

void show ( String s , boolean b , int... arr ) // Valid

void show ( int... arr , String s , boolean b ) // Invalid

An example to show how VAR-ARGS is working

public class prog {

void add(int... b){        // overloaded method with VAR-ARGS
for(int i=0;i<b.length;i++) {
System.out.println(b[i]);
   }
}
public static void main(String sush[]){

 prog p = new prog();
        p.add(10,20,30,40,50);   // passing direct value 

   }
}

In the above code add(VAR-ARGS ) is used where we are passing direct value and overloaded method is with variable argument as an parameter. Internally compiler convert the direct value into an array object. To make you sure i m giving the decompiled code by the java Decompiler of the above program compiled code.

public class prog{

  void add(int[] paramArrayOfInt) {
      for (int i = 0; i < paramArrayOfInt.length; i++)  {

      System.out.println(paramArrayOfInt[i]);
     }
   }

  public static void main(String[ ] paramArrayOfString) {

   prog localprog = new prog();
   localprog.add(new int[]{10,20,30,40,50}); //Creating new 
                                               array object
   }
}

Here you can see that a new array object is created by the compiler itself and passing that array object reference to the overloaded add() method. 
Method overloading in java is the facility which allow us to write same name method with different arguments and hence different implementation.So now with the help of method overloading we can use same method name with different implementation and method overloading should we within same class. Let me explain you with the help of an example.

public  class prog{

void display() {
System.out.println("Method without argument") ;
}

void display(int x) {
System.out.println("X="+x) ;
}

void display(String x){
System.out.println("X="+x) ;
}

void display(String y, int x){
System.out.println(y) ;
System.out.println(x) ;
}

public static void main(String sush[ ]) {

  prog p = new prog();

  p.display( ) ;

  p.display(10) ;

  p.display("sushant") ;

  p.display("sush",20) ;

   }

}

In the above code we can see that display ( ) is overloaded with different arguments.

Important point regarding overloading

1> If we are passing int value then it will search for any overloaded method with integer argument, if not available then it will search for any method with its just higher type like long and again if it is also not available then it will search for its wrapper class definition Integer .For ex-

public  class prog{

void display(Integer x){
System.out.println("Integer="+x);
}

void display(long x){
System.out.println("Long="+x);
}

public static void main(String sush[ ]) {

  prog p = new prog();

  p.display(10);

   }

}

In the above example display method is overloaded with Integer (wrapper class) and long as an argument and from main( ) we had passed the value as int type then it will search for it wider type i.e long , if available then it will execute else it will search for its wrapper class.

Rules to be followed while doing overloading 

1> Method name must be same.
2> Return type can be different.
3> Modifier can be different.
4> Parameters must be different as follows.
     a> Types of parameter.
     b> Number of parameter.
     c> Order of parameters type.

Note 1 : Method overloading will be used to achieve static / compile type ploymorphism. 

Note 2 : Try to execute the program passing byte value and defining overloaded with byte type , short type, int type, long type and lastly its wrapper type then if 

a> Overloaded method with byte type is available then it will execute that method only and if not available then it will search for its just higher i.e short

b> short is also not available then it will search for its more higher type i.e int type. If available then it will execute.

c> int is not available then it will search for its more n more higher type i.e long . If available then it will execute.

d> long is not available then it will search for overloaded method with its wrapper type i.e Byte. If available then it will execute.

e> Wrapper class in not available then it will search for VAR-ARGS ( Variable arguments ) . To know more about VAR-ARGS Click Here ! .
In java we can define object as the instance of class. Object is used to instantiate all the non-static members. In java everything is based on Object, it means memory are allocating for the objects only. Everything we are dealing with is the help of object only , it may be calling method, accessing instance variable and many more. 

Important point regarding Object in java

1> Object creation is with the use of new operator.

2> All the objects are created in the heap memory which is a part of main memory.

3> Whenever we are creating JVM will first call to default constructor if no constructor in defined in the class to initialize the instance variable. 

4>We can define object for any class, it may be predefined class or use-defined class. 
For ex-There is predefined classes available  for every primitive data type in Object class so that we can use create object for primitive type as well called as wrapper class.

Syntax for defining Object :
< class_name >   <reference_variable >  =  new  < class_name >( [value] ) ;
for ex- Hello h = new Hello( ) ;

In the above syntax we can see that class_name is user defined class and with the new operator we had defined a constructor of the class where we can pass value as well but it is optional. 

5> Reference value is always pointing to the object of the class with contain the reference value where the object is stored in the memory.

6> If not object is created then reference variable is containing NULL as the reference value indicating not pointing any object.

Let me make you more understood with the help of an example :

public class prog{

int x ;                    // instance variable

String name ;             // instance variable

long ln ;                // instance variable

float f ;               // instance variable

void display(){         // Instance method

System.out.println("Display method");   

}


public static void main(String sush[ ]) {

prog p = new prog( );           // Object created

p.display();                  // Instance method called

    }
}

Here variable " x ", " ln "," f " and " name " is instance variable and display ( ) is an instance method . It is only called with the help of object only. 

Here we create and object in the main ( ) by which JVM will internally call a default constructor which is responsible for initializing instance variable with the default value if not initialized by the programmer manually. 

Diagrammatic representation of Object 

Representation of an Object
Here we can see that as per above example " p " is a reference variable indicating the object which contain the instance variable stored in the heap memory.




You all must be wondering that what happened to instance method  ?
Instance method will be called by the object and get executed in the stack memory always. And it is only executed when we are calling method manually in our program with the help an object otherwise it won't execute self. 

Note : If we are not using any new operator then it will remain as a reference value which store null, means not pointing any object.
Before understanding about final method you need to know about over riding because any method in java is having some concern with over riding . So to know more about over riding just Click here. As we know that we can easily over ride non-final method but once method is declared as final it cannot be over ride.And we are over riding any method of super-class in the sub-class only. For ex-

Syntax of Final method :
< final >  < return_type > method_ name ( ) { 


class A{

void display(){            // Non-Final method of super-class
System.out.println("superclass method");

    }
}

public class prog7 extends A{

void display(){          // Non-Final method of super-class

System.out.println("sub-class method");

}

public static void main(String sush[]) {

prog7 p = new prog7();

p.display();

   }

}

In the above class A is a super class and Class prog7 is a sub-class extending A . Here we had defined display() with same return type and no argument , the only change is the implementation. So for over riding we need to care about all these things i.e every thing related to method should be same but with different implementation.

Here we are over riding super-class method i.e display() , and if we try to execute the above code it will execute the statements of sub-class display method and print "sub-class method" .

What if we make the display() as final ? will it be over ride or not ?
No it will not override because once it declared as final it cannot be modified further in the sub-class with different implementation. For ex-

class A{

final void display(){        // Final method of super-class

System.out.println("superclass method");

    }

}

public class prog7 extends A{

void display(){           // Cannot be over ride in sub-class

System.out.println("sub-class method");

}

public static void main(String sush[]) {

prog7 p = new prog7();

p.display();

   }

}

While compiling the above code compiler give the complication error saying "display() in prog7 cannot override display() in A; overridden method is final " .
In java there is no such word like constant to define a variable as constant. But java has a keyword called final which is used to declare constant in Java. Like C in java also by declaring a variable as a constant cannot be modified further and if tried then it will give compilation error. Final is also called as the modifier in java like static is a modifier.

Syntax of final variable :

< final > < data_type > < var_name > = < value > ;

for ex-   final   int   x  =  20 ; 

Important points regarding final variable :

1> As it is a constant in java so it must be initialized while declaring. If it is not initialized and tried to access then it will give run-time error as "  variable might not have been initialized " . For ex-


public class prog6{

public static void main(String sush[]) {

final int x ;              // final variable not initialized

System.out.println(x);    // Run-time error 

    }
}

Now think why it is not giving compilation error as we are very sure that final variable must be initialized at any cost because value once assign cannot be modified ?

2 > It is not giving compilation error because if we are not assigning at the time of declaration then there is a chance of initializing it later in the method block but it must be before using it.  For ex-


public class prog6{

public static void main(String sush[]) {

final int x ;                  // final variable not initialized

x = 40 ;

System.out.println(x); 

    }
}

3 > But the above two statement is only true when we are going to use a local constant. If it is instance local then we must have to initialize at the time of declaration otherwise it will give compilation error as "variable might not have been initialized ".

Why its a compilation error ?
It is because instance variable is by default initialized to zero and compiler is not sure that the programmer will surely initialize it later before using it but the same was not in the same of local variable because its mandatory to initialize local variable as it has not default value. So compiler is not going to take any risk and force the programmer to initialize. For ex-

public class prog41{

final int x;

x=20;                        // Compilation Error 

public static void main(String sush[]) {}
}


4 > If we try to modify it then it will give the compilation error as " variable might already have been already assigned " . For ex-

public static void main(String sush[]) {

final int x =40 ;            // Initialized final variable

System.out.println(x++);    // Compilation error 

}

5 > If we try to use any arithmetic operation with the final variable while accessing it, it won't give any compilation as because we are not modifying the constant rather we are just accessing a constant value with another. For this, compiler is having a important role . For ex-

public static void main(String sush[]) {

final int x =40 ;            // Initialized final variable 

System.out.println(x+20);   // Here it is not modifying the final variable. 

}

You must be thanking of why it is not giving any compilation error are we modifying final variable . Actually it seems modifying but in real we are not modifying. But how ?
Whenever we are trying to access any final variable the task of the compiler is to put the direct assigned final value rather using it with variable. So in the above program compiler place 40 in place of " x " and now we left with " 40 + 20 ", hence we can add two constant easily .

Now you must be wonder how do we confirm the above written statement ?
Use any java decompiler it will decode your class file and we also know that .class file is generated by the compiler which is more optimized form of our source file . Here is the decompiled code of above program.

public class prog6 {

  public static void main(String[] paramArrayOfString) {

    System.out.println(60);

  }
}
Here you can see that compiler just replaces the above code of " x + 20 " by 60 . So it is directly using value instead of using through variable.

6 > We can also use final with static with a variable to make it a class variable.

What is the use of making it a class scope ?

-- Defining final variable within a method will limit the accessibility within the method block only.
So what if we want to access outside the method ?
For this we have two option:
 a> Using instance final variable i.e accessibility with the Object.
 b> Using a class final variable i.e accessibility with class name no need of Object.

So if we have all member declared as a static in our program then no need to create an object for accessibility of members , we can use directly with the class name. 



Note : Final static variable should not be declared within any method , it should be a class variable otherwise give compilation error " modifier static not allowed here".
Loading the bean which is configured in the Spring-context.xml file, can be done in two ways :

1. Eager loading or Aggressive loading
2. Lazy Loading
Eager loading or Aggressive loading 
In the case of Aggressive loading all the beans will be loaded and initialized at container start-up. It is called as eager or aggressive loading because of loading all the bean , instance instantiated and initializing at the container start-up only. We just have to write some configuration in the XML file to indicate container for eager loading. This loading is at the bean level . 

To load any bean aggressively we have to mention lazy-init="false" . 
Note that this configuration is at the bean level that means if we want to load all the configured bean to load eagerly then have to mention in each bean tag. If not mention then also by default it load eagerly.  

Lazy loading 
In the case of lazy loading all the beans will be loaded, instantiated and initialized when the container try to use them by calling getBeans() method. We just have to mention lazy-init="true" in order to load the beans lazily. 
So bean will be loaded according to the configured lazy-init attribute with its corresponding value , or if not mentioned then by default it load Eagerly.  

Note : All bean having singleton scope will follow aggressive loading and all bean with prototype scope will follow lazy loading.

To know more about bean scopes - Click Here
Bean instance created by spring container can be in one of the following scopes.
  1. Singleton
  2. Prototype
  3. Request
  4. Session
  5. Global-Session
Singleton : When bean scope is singleton only one instance will be created for that bean and the same instance will be returned when you call getBean() method. Singleton is the default scope in ApplicationContext container. When scope of bean is singleton than default loading type is aggressive loading.

Prototype : When bean scope is prototype then new instance will be created for that bean when you call getBean() method. When scope is prototype the default loading type is lazy loading.

Request : Request scope is equal to HttpServletRequest in web application it means for every new request a new instance will be created.

Session : Session scope is equal to HttpSession scope in web application. It means for new instance per user.

Global-Session : Global-Session scope is equals to session in portlet based web application.
Now lets have some useful stuffs regarding initialization block. It is just a block defined as the static. We can have print statement ,  static method call but we cannot declare any static variable in static initialization block. When any class is loaded then first JVM will initialize the static variable and then execution starts and initialization block statements are first  to be executed by the JVM even before executing main ( ) .

Syntax of static initialization block :
< static > {  } 

 Let me make you clear with the help of an example.

public class prog4 {

static int i ;

static {

System.out.println("Static initialization Block");

}

public static void main(String sush[]) {

System.out.println(i);

    }

}

In the above example we can see that after declaring static variable a static block is defined and then main method.

Steps for the execution of the above program : 


1 >Here first JVM will load the .class file and while loading it will first initialize the static variable. By default its value is zero.

2> After initializing static variable it will start execution and first it will execute the all the static block defined in the program.

3> After executing it will execute the main method. But if there is any function call in the static block then the control will be transferred to the static method. 

Like if we want to store the return value from a method to the static variable then it will first execute the method then assign the value to the static variable and then the execution will start with the static block if any.
For ex-

public class prog4 {

static int i=value() ;

static int value (){
return 2 ;

static {
System.out.println("Static initialization Block");
}
public static void main(String sush[]) {

System.out.println(i);
    }
}

Steps of execution for the above program :

1 >First JVM will call the value() and then assign 2 to the static variable " i ". 

2> After assigning value to the variable it will execute the static block and print the " initialization block ".

3> Then execute the main () and print the value of i as " 10 " .

Note : If we have multiple static block then execution will follow the order of declaration of static block . Execution will be top to bottom from the class declaration . And if any sub class extending super class then all super class static block will be executed first then all sub-class static block. 

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic