Sunday, 28 September 2014

Final Variable With Example


There is a final variable a, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

Example Program

class Super
{
                       final int a=100;
                       void show()
                       {
                        //a=200;error , can't be changed because final variable once assigned a value can never be changed.

                        System.out.println("Welcome to the final Keyword                       "+a);
                       }
}

class Demofinal
{
                       public static void main(String args[])
                       {
                        Super obj=new Super();
                        obj.show();
                       }
}


Output

D:\>javac Demofinal

D:\>java Demofinal

Welcome to the final Keyword    100

Wednesday, 24 September 2014

Constructor Overloading with Example

Constructor Overloading

Constructor overloading in java allows to have more than one constructor inside one Class. 

Example Program

class Demo
{
                       Demo()
                       {
                        System.out.println("Constructor without parameter");
                       }
                       Demo(int a)
                       {
                        System.out.println("Constructor with integer parameter "+a);
                       }
                       Demo(float b)
                       {
                        System.out.println("Constructor with float parameter "+b);
                       }
}
public class Democonover {
                       public static void main(String args[])
                       {
                        Demo obj1=new Demo();
                        Demo obj2=new Demo(4);
                        Demo obj3=new Demo(5.9f);
                       }
}


Output

D:\> javac Democonover.java
D:\>java Democonover
Constructor without parameter
Constructor with integer parameter 4
Constructor with float parameter 5.9


Explanation



Tuesday, 23 September 2014

Constructor program with parameter passing

 Constructor program with parameter passing

 Program

class Parmpass
{
          int i;
          Parmpass(int a)
          {
                   i=a;
                   System.out.println("Constructor with parameter "+i);
          }
}
class ParmpassMain
{
          public static void main(String args[])
          {
                   int b=10;
                   Parmpass obj= new Parmpass(b);
          }
}

Output   

D:\>java ParmpassMain
Constructor with parameter 10


Explanation