Java programming example objects + Static

Embed Size (px)

Citation preview

  • 8/17/2019 Java programming example objects + Static

    1/3

    Example 1

    class Rectangle {

      int length;

      int width;

      Rectangle(int l, int b) {  length = l;

      width = b;

      }

      void area(Rectangle r1) {

      int areaORectangle = r1!length " r1!width;

      #$stem!o%t!println(&'rea o Rectangle & 

     areaORectangle);

      }}

    class Rectangle*emo {

      p%blic static void main(#tring args+) {

      Rectangle r1 = new Rectangle(1-, .-);  r1!area(r1); 

    }}

    O%tp%t o the program

    Area of Rectangle : 200

    Example 2

    import java.io.File;

    import java.io.IOException;

    class Rectangle {  int length;

      int rea!th; 

    Rectangle"int l#int $ {

      length % l;

      rea!th % ;

      }

      Rectangle getRectangleOject"$ {  Rectangle rect % new Rectangle"&0#20$;

      ret%rn rect;

      }}

  • 8/17/2019 Java programming example objects + Static

    2/3

    class RetO {

      p%blic static voi! main"#tring arg'()$ {

      Rectangle o& % new Rectangle"*0#+0$;  Rectangle o2;

      o2 % o&.getRectangleOject"$;  #$stem.o,t. println"-o&.length : -  o&.length$;

      #$stem.o,t. println"-o&.rea!th: -  o&. rea!th$;

      #$stem.o,t. println"-o2.length : -  o2.length$;

      #$stem.o,t. println"-o2.rea!th: -  o2. rea!th$;

      }}

    O,tp,t of the /rogram :

    o&.length : *0

    o&.rea!th: +0o2.length : &0

    o2.rea!th: 20

    Example

    class 1ifference { 

    p%blic static void main"tring() arg'$ {

      !i'pla3"$;  //calling without object   1ifference t % new 1ifference"$;

      t.'ho4"$;  //calling using object 

      } 

    static void !i'pla3"$ {

      3'tem.o,t. println"-/rogramming i' ama5ing.-$;

      } 

    void 'ho4"${

      3'tem.o,t. println"-6ava i' a4e'ome.-$;  }

    }

    Example *

  • 8/17/2019 Java programming example objects + Static

    3/3

    cla'' 1ifference {

      'tatic voi! !i'pla3"$ {  3'tem.o,t.println"-/rogramming i' ama5ing.-$;

      }

      voi! 'ho4"${  3'tem.o,t.println"-6ava i' a4e'ome.-$;

      }}

    cla'' 1ifference7e't1rive{

      p,lic 'tatic voi! main"tring() arg'$ {

      1ifference.!i'pla3"$; 88calling 4itho,t oject

      1ifference t % ne4 1ifference"$;  t.'ho4"$; 88calling ,'ing oject

      }

    }

    Example +

    88/rogram of 'tatic variale 

    class t,!ent9{

    int rollno;

    tring name;

    static tring college %-I7-;

    t,!ent9"int r#tring n${rollno % r;

    name % n;

    }

    void !i'pla3 "${3'tem.o,t.println"rollno- -name- -college$;}

    p%blic static void main"tring arg'()${

    t,!ent9 '& % new t,!ent9"&&-aran-$;t,!ent9 '2 % new t,!ent9"222#-Ar3an-$;

    '&.!i'pla3"$;'2.!i'pla3"$;

    }

    }

    /est it 0ow

    O,tp,t:&&& aran I7

      222 Ar3an I7

    http://www.javatpoint.com/opr/test.jsp?filename=Student8http://www.javatpoint.com/opr/test.jsp?filename=Student8