SHARE
TWEET

cancer

a guest Oct 17th, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  2.  
  3. public class MyClass extends Actor
  4. {
  5.     int privateNumber = 0;
  6.     static int publicNumber =0;
  7.    
  8.     public MyClass(){//creating new instance variable
  9.         privateNumber++; //increase privateNumber for instane variable
  10.         publicNumber += privateNumber; //NON MOVING, public number = instance variable + itself
  11.     }
  12.    
  13.     public MyClass(int privateNumber){//creating new instance variable with privateNumber
  14.         privateNumber++;// increase privateNumber by 1, useless in second b
  15.         publicNumber += privateNumber; // update NONMOVING, static number
  16.     }
  17.    
  18.     public void printing(){
  19.         System.out.println(privateNumber + publicNumber);
  20.     }
  21.    
  22.     public static void test(){
  23.         MyClass a = new MyClass(); //a.privateNumber = 1, publicNumber =1
  24.         System.out.println(a.privateNumber + publicNumber); //1+1
  25.        
  26.         //IF(TRUE)
  27.         int fake = 3;//decoy static number
  28.         MyClass Bdecoy = new MyClass(fake); //Bdecoy.privateNumber = 0, publicNumber = 5
  29.         System.out.println(Bdecoy.privateNumber + publicNumber);//0 + 5
  30.        
  31.         //MyClass b = new MyClass(data2)
  32.         MyClass b = new MyClass(publicNumber); //publicNumber = 5
  33.        
  34.         //b.check
  35.         System.out.println(b.privateNumber + publicNumber);//6+5
  36.        
  37.         //b.reset
  38.         b.privateNumber = 0; publicNumber =0; //b.publicNumber = 0, b.privateNumber =0
  39.        
  40.         //a.check
  41.         System.out.println(a.privateNumber + publicNumber);//1+0
  42.        
  43.         //b.check
  44.         System.out.println(b.privateNumber + publicNumber);//0+0
  45.     }
  46.    
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top