java - Accessing data from other classes -


im beginner please very specific. anyway have 3 code classes here , want acces data c-b b-a e.g.

class goobypls { {     private int chealth = 20;     private int mhealth = 20;     private int cagility = 10;     private int magility = 10;     private int cdefence = 5;     private int mdefence = 5; }    }  class stats { public static void foo() {     string health =  chealth + "/" + mhealth ;     string agility = cagility + "/" + magility;     string defence = cdefence + "/" + mdefence; }  }  class viewstats { public static void foo() { system.out.println("health");                 system.out.println(health);                 system.out.println(" ");                 system.out.println("agility");                 system.out.println(agility);                 system.out.println(" ");                 system.out.println("defence");                 system.out.println(defence);                 system.out.println(" ");   } } 

so goobypls a, stats b , viewstats c

also cant put in 1 class because there modifier class edit chealth, mhealth etc

in class goobypls add getters each item following:

class goobypls { {    private int chealth = 20;     public int gethealth(){       return chealth;    } } 

and in viewstats this:

goodypls gp = new goobypls(); system.out.println(gp.gethealth()); 

just call getters whenever when want use private variable. or can define variables public, can directly call them in viewstats. getters , setters better design point of view, since hiding information better. fields should declared private unless there reason not doing so.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -