Let's say I can use both private or public and it doesn't make any difference for me to choose between.
For memory usage which one is better?Why?
Let's say I can use both private or public and it doesn't make any difference for me to choose between.
For memory usage which one is better?Why?
No, in a class the property modifier of public or private does not change the memory footprint.
The number of bytes set aside for the item is simply related to the size of the type (int, long, etc) in memory.
The modifier only limits the fact that the memory address is not accessible outside the class (in the case of private).
So if you have a class like:
class Point
{
private int x;
public int y;
}
Both of those variables take the same number of bytes when accessed. However to access y you can do so like the following:
Point p = new Point();
p.y = 55;
However, you cannot do that with x since it is private.
You can access x from within the code inside the class like the following though.
class Point
{
private int x;
public int y;
public Point ()
{
// this is the contstructor but other member functions woud
// work too
this.x = 77;
// or
x = 77;
}
}
from this answer