Results 1 to 7 of 7
Thread: Quick Optimizing question
- 04-15-2011, 01:07 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 48
- Rep Power
- 0
Quick Optimizing question
Im fairly new to java and im an optimize freak. Say i have objects that each have its x,y, speed x, speed y, color, etc. Would it be faster to access those variables by setting them as public or making functions to return each specific variables? Or if you have a better suggestion to how to store lots of variables, tell me!
- 04-15-2011, 01:09 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
If you have a class with these variables you want to encapsulate them by making them private. You can directly access them inside the class, and use setters and getters to handle them from outside of the class.
-
Please read the section titled "Write Dumb Code":
Writing Better Code: A Conversation With Sun Microsystems Technology Evangelist Brian Goetz
- 04-15-2011, 01:11 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 48
- Rep Power
- 0
- 04-15-2011, 01:15 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
I believe it should be roughly the same speed, it may be a tiny bit faster to access them directly because it doesn't have to call the method, the difference in speed won't be noticeable. You probably don't want to be optimizing something like this unless you absolutely need to increase performance. Also check out fubars link.
- 04-15-2011, 01:18 AM #6
I once read someone say micro-optimisations make puppies cry.
By making your instance variables public the gain is only miniscule. On the other hand you break the concept of encapsulation. Another thing to consider is if you make the variable only acessible via a setter is that you can control what values can be assigned to the variable.
- 04-15-2011, 02:25 AM #7
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
Heh I thought about that question myself when I was starting out Java and writing getters and setters for private instance variables. Of course its faster to have them as public, as then the JVM will avoid putting that method and its local variables on the stack, which would take about 3 operations per access (push method to stack and local vars to stack, get var, pop the method off the stack...something along those lines).
Faster yes, but by a minuscule amount. + Your variables are exposed for all the other classes to see, and any other programmer using your class can do
sgthale.money = -1000000;
With a setter of course, you wouldn't allow that :D.
Similar Threads
-
quick question
By biggerthanblue in forum New To JavaReplies: 2Last Post: 04-10-2011, 05:33 AM -
Quick question
By Thumper in forum New To JavaReplies: 10Last Post: 11-07-2010, 11:06 PM -
Quick Question...
By FatalSylence in forum New To JavaReplies: 4Last Post: 10-15-2010, 03:38 PM -
Hello everyone! quick question.
By irishhokie in forum New To JavaReplies: 5Last Post: 04-03-2009, 05:13 AM -
Quick Question
By Graeme in forum New To JavaReplies: 4Last Post: 01-08-2009, 09:01 PM
Bookmarks