Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Use of 'this' operator
  2.  
  3.     // class Rectangle2 shouldn't be declared public
  4.     class Rectangle2 {
  5.     int length,breadth;
  6.     // 'this' is equivalent to self in python
  7.     void show(int length,int breadth) {
  8.         this.length = length;
  9.         this.breadth = breadth;
  10.     }
  11.     // because I have 'this'ed length and breadth above, I am not passing any
  12.     // parameters for the method 'area' below
  13.     int area() {
  14.         return (length * breadth);
  15.     }
  16.  
  17. }
  18.     public class This {
  19.     public static void main(String[] args) {
  20.         Rectangle2 rect = new Rectangle2();
  21.         rect.show(2,3);
  22.         int area = rect.area();
  23.         System.out.println("Area --> "+area);
  24.     }
  25.  
  26. }