2

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?

Vazir
  • 61
  • 1
  • 4
  • 1
    What do you mean _memory-wise_? Why do you have doubts? – Sotirios Delimanolis Jun 23 '15 at 18:45
  • which would be using less of my computer's memory? I mean if I can define something with private? Should always go for it? Because it uses less of my computer's memory? – Vazir Jun 23 '15 at 18:49
  • I have read hours and hours and tried applying my limited knowledge, yet again some geniuses are not appreciated because I am not good as they are! Am I at the wrong place to be asking these questions? I am frustrated with the negatives I get each time for simply trying! – Vazir Jun 23 '15 at 18:57
  • I don't believe you've done any research, for any amount of time. **Prove me wrong.** You assume you've asked a good question. 4 people think otherwise. Take the tour and read the help center. There is information there that will help you write a proper question. – Sotirios Delimanolis Jun 23 '15 at 19:01
  • @SotiriosDelimanolis put yourself in my shoes... and only compare me with people of my own league. If you compare me with people like you then you that's a silly question...but you are like 6 leagues/years ahead of me! I read [link](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private?rq=1) and also am using a 17 hour online course and did some more googling but didn't find anything. And the duplicate question's link is for PHP which I am not familiar with so much. – Vazir Jun 24 '15 at 13:01
  • You misunderstand how Stack Overflow works. It's bot about the user, it's about the quality of the question (or answer). If you've done research, show the fruits of that research **in your question**. It's useless in a comment. You'll notice the answer in the duplicate also addresses Java. – Sotirios Delimanolis Jun 24 '15 at 13:59
  • @SotiriosDelimanolis I do notice now **because** of asking this question...prior to that with my limited knowledge, I didn't know that PHP and Java are related in that sense, I have to look into it. I believe that the system is very harsh for newbies...somehow un-penetrable. Obviously I will be asking lame question, because I am not in the level of the pros...For next time I will give more explanation of my research to make it look less lame as you instructed. Thanks – Vazir Jun 24 '15 at 16:29
  • Also, take the Tour and read the Help Center. – Sotirios Delimanolis Jun 24 '15 at 16:39
  • 1
    How on earth php and Java is same ? Come on, it's not duplicated. – 林果皞 Feb 15 '17 at 11:03

3 Answers3

4

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;
     }
}
raddevus
  • 8,142
  • 7
  • 66
  • 87
3

No, access modifiers have no effect on runtime memory utilization in either Java or PHP, nor in any other language I have heard of. Possibly the code size may increase a few bytes due to access modifiers in some bytecodes depending on how they are encoded. Your program must be extremently efficient in other respects before it is worth worrying about this.

from this answer

Community
  • 1
  • 1
discipliuned
  • 916
  • 7
  • 13
1

These are access modifiers. Memory-wise (RAM) no, there is no difference. This does not happen with the static keyword. Using static keeps the object in the memory.

George Daramouskas
  • 3,720
  • 3
  • 22
  • 51