Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- int counter = 0;
- public static void main(String[] args) {
- int count = 0;
- Test [] m4a = new Test[20]; // make an array
- int x = 0;
- while (x < 20) { // loop x is 0 to 19
- // so we do this 20 times
- m4a[x] = new Test(); // make an instance an dput in array
- m4a[x].counter = m4a[x].counter +1; // the counter field
- // of this instance is now 1
- count = count +1 ; // increment count by 1 (adds a total of 20)
- count = count + m4a[x].maybeNew(x); // and maybe
- // maybe is 1 for x 0 to 4 - so add in a total of 5
- x=x+1;
- }
- System.out.println(count + "" + "\t" + m4a[1].counter);
- // outputs 25 1
- }
- public int maybeNew(int index){
- if(index<5){ // so if x is 0 to 4 - that is, 5 times
- Test m4 = new Test(); // this is pointless
- // m4 is a local variable, created on teh stack, and
- // disappears at the endo f this method
- m4.counter = m4.counter + 1;
- return 1; // so we return 1 if index is 0 to 4
- }
- return 0; // otherwise return 0
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment