1

Is there a runtime performance differance between public and private variables/methods?

I know that it is considered good practice to keep things private if possible, but is there any optimisation related reason.

Jamie
  • 81
  • 1
  • 4

3 Answers3

0

I don't think there is directly. Access modifiers are more of a compile-time thing in my view anyway.

Even if there was, don't go that way, there is a very good reason (several of them probably) that you shouldn't make class fields public.

There is an incredibly small performance impact because you have to call the getter and setter methods for a field, but unless you do complex operations there, it definitely won't be noticable. It's a matter of miliseconds at most.

MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • 1
    You don't have to use getters and setters, but not doing so won't improve performance in general and the difference for something this minor is likely to be nano-seconds at worst. It can even be sub-nano-second making it very difficult to even measure reliably. – Peter Lawrey Apr 09 '12 at 07:54
0

Like most of these questions I would say; write clear, simple code and it will perform well also.

If some one tells you something is a good idea for performance reasons, make sure this is backed up with real numbers, is still the case for the version of Java you are using (much of this advise is out of date), and it is appropriate for your application.

Often "performance reasons", is an excuse to write obscure code, when actually it may be no faster or can even be much slower (as it confuses the JVM optimiser, just as it will confuse you)

Some people are so sceptical of performance optimisation that you have the quote "premature optimisation is the root of all evil" This is an exaggeration, but it is a good warning, not to worry about performance concerns unless you really know you need to improve performance, and your changes really make a difference.

To this specific question, you can't call a private method from another class. So basically, you can't from another outer class, and from another class in the same outer class, and accessor is created which would normally be inlined if called enough.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks for the response. I wanted to be able to make informed decisions while writing the program. I was wondering if it might have some effect on garbage collection etc – Jamie Apr 09 '12 at 08:15
  • No, absolutely none. I wouldn't worry about GC issues either unless you know you have a problem because you have measured that you really have a problem. e.g. You used a memory profiler. – Peter Lawrey Apr 09 '12 at 08:29
  • In short with 12 years experience tuning Java applications, I still wouldn't rely on my guesses as to what will make a program faster. I guess what to test next, but I still test it a few different ways to be sure I have the right approach. – Peter Lawrey Apr 09 '12 at 08:31
  • You may find my blog interesting, it has lots of tips about low level Java performance testing and tuning. Google my name to find it. ;) – Peter Lawrey Apr 09 '12 at 08:33
0

There is no difference runtime performance between private and public variables/methods.It only depend on your program requirements. for example, you have a method that is required for the entire program then you should use public method.Its reduce code duplication. But you have a method that is required only one class then you should use private method.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65