Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Variable and Constant in Java:-





Variable or Data Member:-  It is a special identifier whose value can be changed at compile-time or runtime.

Variable declaration Syntax:-

 access modifier + non access modifier +  datatype + identifier

 public                     static         int               x

int x =10 ;    //default instance int x=10 dynamic memory allocation using object

static int y=20 //  default static int y=20 one-time memory allocated at compile time


Types of variable:-

1 Instance or Dynamic:-

It will be declared without any static modifier and it will be allocated memory at the time of execution by the object.

a variable which will be declared underclass or under instance method without using static modifier will be instance variable


instance variable will never declare under static method but it can be used.

this type of variable is also called dynamic data members in OOP'S Programming. means non-static, instance, dynamic all are the same.


Example of nonstatic variable:-


class Dynamic
{
   int a,b,c;
   
    public static void main(String args[])
    {
      Dynamic obj= new Dynamic(); 
      obj.a=100;
      obj.b=200;
      obj.c=obj.a+obj.b;
      System.out.println(obj.c);

    }

}

class A
{
    int x=10; //global instance variable
    void fun()
    {
       int y=20; //local instance variable
    }

}

A obj = new A();  //object


Complete Explanation by Program:-

class Add
{
   int a=100,b=200,c;
   public static void main(String args[])
   {
      Add obj = new Add();
      obj.c=obj.a+obj.b;
      System.out.println(obj.c);
      Add obj1 = new Add();
      obj1.a=10;
      obj1.b=20;
      obj1.c=obj1.a+obj1.b;
      System.out.println(obj1.c);
   } 


}



2 Static:-

This type of variable will allocate memory after compile-time, static has constant memory but not the constant value, a value can be changed in the same memory multiple times.

static datatype identifier=value

static int x=100;
x=10;
x=12;


we can define static variable underclass using static keyword and static method without using the static keyword. if we define a static variable under the static method then no need to declare a static keyword.


class A
{
    static int a=100;
    void fun()
    {
        int b=20;
    }

   public static void main()
   {
      int c=30;
   }

}

Program of Static With Complete explanation?

class Add
{
   static int a=100,b=200,c;
   public static void main(String args[])
   {
       c=a+b;
      System.out.println(c);
      a=10;
      b=20;
      c=a+b;
      System.out.println(c);
   } 


}


......................................................................................................................................... 

Program explanation in Java:-

class Dynamic
{
    int a,b,c;
   
    public static void main(String args[])
    {
      Dynamic obj=new Dynamic();
      obj.a=100;
      obj.b=200;
      obj.a=20;
      Dynamic obj1=new Dynamic();
      obj1.a=2000;
      obj1.b=3000;
      obj=obj1;
      obj.c=obj.a+obj.b;
   
      System.out.println(obj.c);

    }

}
Constant:-
....................................................................................................................................................

If we can not change the value of the identifier at compile-time or run-time then it is called constant.

java provides the final keyword to a constant declaration.

final static datatype variablename=value;

constant memory always will be static by default.


class ConstDemo
{
   final static int A=100;
     
   public static void main(String args[])
   {
       A=20;
       System.out.println(A);

   }


}





Assignment:-

1)WAP to calculate electricity bill where unit price and total consumption will be assigned by the users?


2)  WAP to calculate salary where basic, ta, da, comm, pf,hra,noofleave will be assigned by the users?

3)  WAP to calculate the multiplication of complex numbers?

The solution to this program:-
class Complex
{

    public static void main(String args[])
    {
       int a,b,c,d,r,i;
       a=2;
       b=3;
       c=4;
       d=5;
       r = a*c-b*d;
       i = a*d+b*c;
       System.out.println(r + "+" + i + "i");
  
    }


}


Program to perform the addition of complex numbers?

class Complex
{

    public static void main(String args[])
    {
       int a,b,c,d,r,i;
       a=2;
       b=3;
       c=4;
       d=5;
       r = a+c;
       i = b+d;
       System.out.println(a + "+" + b + "i");
       System.out.println(c + "+" + d + "i");
       System.out.println(r + "+" + i + "i");
  
    }


}

4)  WAP to find the maximum number in any three-digit number?
int a= 457 ;  //7


5)  WAP to find middle numbers using three-digit numbers?


6)  WAP to calculate the area of a circle where pi will be constant and radius will be variable?


7)  WAP to calculate simple interest using instance type variable?


8)  WAP to convert temperature from Celsius to Fahrenheit using static data members globally?


c/5 = f-32/9














39 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. class Electro
    {
    public static void main(String args[])
    {
    int up=10,c=300;
    int eb=up*c;
    System.out.println(eb);
    }
    }

    ReplyDelete
  2. class Maxt
    {
    public static void main(String args[])
    {
    int a=247;
    int d1,d2,d3,max;
    d3=a%10;
    a=a/10;
    d2=a%10;
    a=a/10;
    d1=a;
    max=d1;
    if(max<d2)
    {
    max=d2;
    }
    if(max<d3)
    {
    max=d3;
    }
    System.out.println("Highest Digit is: "+max);
    }
    }

    ReplyDelete
  3. class Middle
    {
    public static void main(String args[])
    {
    int a=247;
    int m;
    a=a/10;
    m=a%10;
    System.out.println("Middle Digit is: "+m);
    }
    }

    ReplyDelete
  4. class Area
    {
    public static void main(String args[])
    {
    double r=14;
    double area= Math.PI*r*r;
    System.out.println("Area of circle with radius "+r+" is: "+area);
    }
    }

    ReplyDelete
  5. import java.util.Scanner;
    class complexnum
    {
    public static void main(String[]arg)
    {
    int a,b,c,d;
    Scanner sc=new Scanner(System.in);

    System.out.println("enter the value of a");
    a=sc.nextInt();
    System.out.println("enter the value of b");
    b=sc.nextInt();
    System.out.println("enter the value of c");
    c=sc.nextInt();
    System.out.println("enter the value of d");
    d=sc.nextInt();
    int r=a*c-b*d;
    int i=a*d+b*c;
    System.out.println(r+"+"+i+"i");
    }
    }

    ReplyDelete
  6. Q:- Example of non-static variable :-
    Solution:-
    class Dynamic
    {
    int a,b,c;

    public static void main(String args[])
    {
    Dynamic obj= new Dynamic();
    obj.a=929;
    obj.b=584;
    obj.c=obj.a+obj.b;
    System.out.println(obj.c);
    }
    }

    ReplyDelete
  7. Q:- Complete Explanation of non-static Vairiable by Program:-
    Solution:-
    class Add
    {
    int a=929,b=854,c;
    public static void main(String args[])
    {
    Add obj = new Add();
    obj.c=obj.a+obj.b;
    System.out.println(obj.c);
    Add obj1 = new Add();
    obj1.a=25;
    obj1.b=75;
    obj1.c=obj1.a+obj1.b;
    System.out.println(obj1.c);
    }
    }

    ReplyDelete
  8. Q:- WAP to calculate the multiplication of complex numbers ??
    Solution :-
    class Complex
    {

    public static void main(String args[])
    {
    int a,b,c,d,r,i;
    a=2;
    b=3;
    c=4;
    d=5;
    r = a*c-b*d;
    i = a*d+b*c;
    System.out.println(r + "+" + i + "i");
    }
    }

    ReplyDelete
  9. Q:- Program to perform the addition of complex numbers ??
    Solution:-
    class Complex
    {

    public static void main(String args[])
    {
    int a,b,c,d,r,i;
    a=2;
    b=3;
    c=4;
    d=5;
    r = a+c;
    i = b+d;
    System.out.println(a + "+" + b + "i");
    System.out.println(c + "+" + d + "i");
    System.out.println(r + "+" + i + "i");
    }
    }

    ReplyDelete
  10. class Ebill
    {
    public static void main(String args[])
    {
    int tunit=100, punit=4, ebill;
    ebill=tunit*punit;
    System.out.println(ebill);
    }
    }


    ReplyDelete
  11. // maximum number in any three-digit number
    class MaxNumber
    {
    public static void main(String args[])
    {
    int num=156;
    if((1>5) && (1>6))
    {
    System.out.println(" 1 is maximum number ");
    }
    else if(5>6 && 5>1)
    {
    System.out.println(" 5 is maximum number ");
    }
    else
    {
    System.out.println(" 6 is maximum number ");
    }
    }
    }

    ReplyDelete
  12. //Multiplication of complex numbers

    class ComplexMul
    {
    public static void main(String args[])
    {
    int a=3, b=4, c=3 ,d=4, e, f;
    e = a*b;
    f= c*d;
    System.out.println(e +"+" +f + "i^2");
    }
    }

    ReplyDelete
  13. //Maximum number in any three-digit number

    class MaxNumber
    {
    public static void main(String args[])
    {
    int num=156;
    if((1>5) && (1>6))
    {
    System.out.println(" 1 is maximum number ");
    }
    else if(5>6 && 5>1)
    {
    System.out.println(" 5 is maximum number ");
    }
    else
    {
    System.out.println(" 6 is maximum number ");
    }
    }
    }

    ReplyDelete
  14. // Middle numbers using three-digit number.


    class MiddleNumber
    {
    public static void main(String args[])
    {
    int number=369;
    int middlenumber;
    number = number/10;
    middlenumber = number%10;
    System.out.println("Middle Number = "+middlenumber);
    }
    }

    ReplyDelete
  15. //Calculate the area of a circle where pi will be constant and radius will be variable.
    class AreaCircle
    {
    static final float pi=3.14F;
    public static void main(String args[])
    {
    int r =20;
    float area;
    area=pi*r*r;
    System.out.println("Area of circle =" +area);
    }
    }

    ReplyDelete
  16. // Calculate simple interest using instance type variable?

    class Sinterest
    {
    int p,t;
    float r;
    float s;
    public static void main(String args[])
    {
    Sinterest obj= new Sinterest();
    obj.p=5000;
    obj.r=0.07F;
    obj.t=5;
    obj.s = obj.p*obj.r*obj.t;
    System.out.println(obj.s);
    }
    }


    ReplyDelete
  17. class Ebill
    {
    public static void main(String[]args)
    {
    int unitrate=10, totalunit=250, bill;
    bill= totalunit*unitrate;
    System.out.println("bill=" + bill);
    }
    }

    ReplyDelete
  18. class Complexadd
    {
    public static void main(String[]args)
    {
    int a=5,b=6,c=8,d=3,r,i;
    r=a+c;
    i=b+d;
    System.out.println(a + "+" + c + "i");
    System.out.println(b + "+" + d + "i");
    System.out.println(r + "+" + i + "i");
    }
    }

    ReplyDelete
  19. class Complexadd
    {
    public static void main(String[]args)
    {
    int a=5,b=6,c=8,d=3,r,i;
    r=a+c;
    i=b+d;
    System.out.println(r);
    System.out.println(i + "i");

    }
    }

    ReplyDelete
  20. class Complexadd
    {
    public static void main(String[]args)
    {
    int a=5,b=6,c=8,d=3,r,i;
    r=a+c;
    i=b+d;
    System.out.println(a + "+" + b + "i");
    System.out.println(c + "+" + d + "i");
    System.out.println(r + "+" + i + "i");
    }
    }

    ReplyDelete
  21. class Complexadd
    {
    public static void main(String[]args)
    {
    int a=5,b=6,c=8,d=3,r,i;
    r=a+c;
    i=b+d;

    System.out.println(r + "+" + i + "i");
    }
    }

    ReplyDelete
  22. class Obi
    {
    int a=7195, b=3595, c;
    public static void main(String[]args)
    {
    Obi obj= new Obi();
    obj.c= obj.a + obj.b;
    System.out.println(obj.c);
    }
    }

    ReplyDelete
  23. class Circle
    {
    final static double pi=3.14
    public static void main(String[]args)
    {
    double r=9, a;

    a= pi*r*r;
    System.out.println("A=" +a);
    }
    }

    ReplyDelete
  24. class Simp
    {
    int p,r,t,s;
    public static void main(String args[])

    {
    Simp obj=new Simp();
    obj.p=50000;
    obj.t=2;
    obj.r=7;
    obj.s=((obj.p*obj.t*obj.r)/100);
    System.out.println(obj.s);
    }
    }

    ReplyDelete
  25. class Mn
    {
    public static void main(String arg[])
    {
    int a=457;
    int b=a/10;
    int m=b%10;
    System.out.println(m);
    }
    }

    ReplyDelete
  26. class Area
    {
    public static void main(String arg[])
    {
    final int p=5;
    int r=2;
    int a=p*r*r;
    System.out.println(a);
    }
    }

    ReplyDelete
  27. class Sinterset
    {
    int p,t;
    float r;

    float s;
    public static void main(String arg[])
    {
    Sinterset obj=new Sinterset();
    obj.p=2000;
    obj.r=2;
    obj.t=2;
    obj.s=(obj.p*obj.r*obj.t)/100;
    System.out.println(obj.s);

    }}

    ReplyDelete
  28. class Temp
    {
    static float tc=50;
    static float f=tc*9/5+32;
    public static void main(String arg[])
    {

    System.out.println(Temp.f+"F");
    }
    }

    ReplyDelete
  29. class Complex
    {
    public static void main(String arg[])
    {
    int a,b,c,d,r,i;
    a=4;
    b=6;
    c=2;
    d=4;
    r=a*c-b*d;
    i=a*d+b*c;
    System.out.println(r+ "+" +i +"i");
    }
    }

    ReplyDelete
  30. class Max
    {
    public static void main(String arg[])
    {
    int a=257;
    int a1,a2,a3,max;
    a3=a%10;
    a=a/10;
    a2=a%10;
    a=a/10;
    a1=a;
    max=a1;
    if(max<a3)
    {
    max=a3;
    }
    if(max<a2)
    {
    max=a2;
    }
    else
    {
    System.out.println("maximum no. is"+max);
    }
    }
    }

    ReplyDelete
  31. class variable
    {
    public static void main(String args[])
    {
    int x=457;
    int y;
    y=x/10;
    int n=y%10;
    System.out.println(n);
    }
    }

    ReplyDelete
  32. class Acr
    {
    public static void main(String args[])
    {
    int r=45,pi=46;
    int a=pi*r*r;
    System.out.println(a);

    }

    ReplyDelete
  33. class A
    {
    int p,t;
    int r;
    int s;
    public static void main(String args[])
    {
    A a=new A();
    a.p=2000;
    a.r=2;
    a.t=3;
    a.s=a.p*a.r*a.t/100;
    System.out.println(a.s);
    }
    }

    ReplyDelete
  34. class cles
    {
    static void main()
    {
    float fah,cel;
    cel=13;
    fah=((cel+9)/5+32);
    System.out.println(fah);
    }
    public static void main(String args[])
    {
    cles a=new cles();
    a.main();
    }
    }

    ReplyDelete
  35. class max
    {
    public static void main(String args[])
    {
    int a=4,b=5,c=7;

    if(a>=b)
    {
    if(a>=c)
    {
    System.out.println("maximum number="+a);
    }
    else
    {
    System.out.println("maximum number="+c);

    }
    }
    else
    {
    if(b>=c)
    {
    System.out.println("maximum number="+b);

    }
    else
    {
    System.out.println("maximum number="+c);
    }
    }
    System.out.println();
    }
    }

    ReplyDelete
  36. class cles
    {

    static float cel=15;

    static float fah=((cel+9)/5+32);

    public static void main(String args[])
    {
    System.out.println(fah);

    }
    }

    ReplyDelete
  37. class Complex
    {
    public static void main(Strings args[])
    {
    int a=2,b=3;//a+ib is complex number
    int c=3,d=2;//c+id is complex number
    int x,y;
    x=(a*c)+((b*d)*-1);
    y= a*d+b*c;
    System.out.println("Result of " +a +"i"+b + "and " + c+ "i"+d+ "is ");
    System.out.println(x+"i"+z);
    }

    ReplyDelete
  38. class Complex
    {
    int p, r, t;
    public static void main(Strings args[])
    {
    float si;
    Complex c= new Complex();
    c.p = 10000,
    c.r=10;
    c.t=2;

    si =(c.p*c.r*c.t)*.01;
    System.out.print(si)
    }

    ReplyDelete

  39. WAP to find the maximum number in any three-digit number?
    int a= 457 ;

    class Bigger
    {

    public static void main(String args[])
    {

    int num,a,b,c;

    a=num%10;
    num=num/10;
    b=num%10;
    num=num/10;
    c=num%10;
    if (a>b)
    {
    if (a>c)
    {
    System.out.println(a)
    }
    else
    {
    System.out.println(c)
    }
    }
    else
    {
    if(b>c)
    {
    System.out.println(b)
    }
    else
    {
    System.out.println(c)
    }
    }
    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post