Results 1 to 7 of 7
  1. #1
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Smile 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!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sgthale View Post
    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!
    Please read the section titled "Write Dumb Code":
    Writing Better Code: A Conversation With Sun Microsystems Technology Evangelist Brian Goetz

  4. #4
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    thanks for the response but my question is if thats faster or slower than just setting them as public :confused:

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.

  7. #7
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    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

  1. quick question
    By biggerthanblue in forum New To Java
    Replies: 2
    Last Post: 04-10-2011, 04:33 AM
  2. Quick question
    By Thumper in forum New To Java
    Replies: 10
    Last Post: 11-07-2010, 10:06 PM
  3. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 02:38 PM
  4. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 04:13 AM
  5. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •