class Ball { public String name = "Billy"; private int ID = 12345; } class BallLoader { Ball loadBall; BallLoader() { loadBall = new Ball() System.out.println(loadBall.name); // this works System.out.println(loadBall.ID); // this will not work and give a compile error } }
Why would you use private? It seems it'd be more trouble to define a getID() method that returns the private variable. It would save you a lot of trouble to just set everything to public.
<font face="Consolas">
<font color="green">// C++ code</font>
<font color="blue">class</font> foo {
<font color="blue">char</font> *data;
<font color="blue">public:</font>
<font color="blue">char</font>* GetData() {
<font color="blue">if</font>(!data)
LoadData();
<font color="blue">return</font> data;
}
};
</font>
For SetData(<font color="blue">char</font>*) you could check if it's actually valid before changing variable.