Posted April 20, 20196 yr I'm relatively new to Java, and to Forge, and this has been bugging me for a bit. Often times I'll realize I have to call a getter function (e.g. Minecraft.getInstance) every time an event is fired. So to get around this I'll sometimes create a field in the class and set it once. Or in the case of a loop I'll store the value in a local variable and refer back to the variable. I'm used to doing this in languages like C, where optimization and garbage collection isn't exactly the same concept as it is in Java. So my question is, how much of this is necessary? Does it really matter if I call the getter functions a bunch? Does it add any overhead? Memory leaks? Anything? Why are there so many? Sorry if this is more of a Java question. I'm really only using Java for Forge and so the answer will have a significant impact on how I write mods. Thanks ?
April 20, 20196 yr Pretty much all getters are internally optimized by the JVM. Edited April 20, 20196 yr by V0idWa1k3r
April 20, 20196 yr Author 14 minutes ago, V0idWa1k3r said: Pretty much all getters are internally optimized by the JVM. So does this mean there's no performance difference between object.field and object.getField()?
April 20, 20196 yr https://stackoverflow.com/questions/23931546/java-getter-and-setter-faster-than-direct-access Basically if you look at the resulting code after a few iterations of it's execution it should be identical in most cases.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.