Player p ; // p is a pointer to a Player. p = new Player(0) ; // Allocate a new object Player q ; // q is another pointer q = p ; // p and q point to same object
int[] a = new int[10] ;
Now a points to an array of 10 unititialized integers.
Player[] a = new Player[10] ;
Now a points to an array of 10 pointers to Players (initially null).
for(int i=0 ; i<10 ; ++i) { a[i] = new Player(i) ; }
Now a points to an array of 10 pointers to different Player objects.
Vect scale( Vect v, double f ) { v.x = f * v.x ; v.y = f * v.y ; f = 0.0 ; // As in C, this has no effect return v ; }
...
q = scale( p, 2.0 ) ; // p and q point to the same object. // The original vector value is lost.
But since arrays are objects, you can sort of fake them.
void halve( int[] ref ) { ref[0] = ref[0] / 2 ; } ... int [] x = new int[1] ; x[0] = 10 ; halve( x ) ;
public class Parent { public int example() { return 0 ; } } public class Child extends Parent { public int example() { return 1 ; } } ... Parent p = new Child() ; System.out.println( "" + p.example() ) ;
public class Foo // Don't forget to declare public classes "public" { public int a ; // Anyone may use a protected int b ; // All derived classes may use b int c ; // Only classes in same package private int d ; // Only within this class }