<<< Constructors revisited     Index     Accessor Operations >>>

8. Constructor Examples


  • 
    public class Point {
        int x;
        int y;
        public Point() {
            x = 0;
            y = 0;
        }
        public Point ( int x, int y ) {
            this.x = x;
            this.y = y;
        }
        public String toString() {
            return x + " " + y;
        }
        public static void main( String[] args )  {
            Point pt = new Point();
            Point pt2 = new Point( 10, 20 );
            System.out.println( pt.toString() );
            System.out.println( pt2.toString() );
        }
    }
    
    
  • To run this example, type command:

        Point.main( null );
    
  • or simply

        java Point
    
<<< Constructors revisited     Index     Accessor Operations >>>