Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3341348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:42:45+00:00 2026-05-18T00:42:45+00:00

I know rule #1 of optimization is: don’t do it! But I figured this

  • 0

I know rule #1 of optimization is: don’t do it! But I figured this was an easy question, and if I start using the faster method now I can save a lot of cpu time when I’m finished.

I’m making an RPG, and let’s say this is part of a custom class:

public class Baddie{

    int health;
    int magic;

    public Baddie(int health, int magic){
        this.health = health;
        this.magic = magic;
    }

    public int getHealth(){
        return health;
    }

Now, the answer to my question may be “there’s no difference” and that’s fine with me.. I just want to know. Is it quicker to get the Baddie’s health using field access:

//Somewhere in the main thread, I get an instance of Baddie..
Baddie b = getScaryBadGuy();
int baddieHealth = b.health;

Or is it quicker to use a return method?

int baddieHealth = b.getHealth();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:42:45+00:00Added an answer on May 18, 2026 at 12:42 am

    Copied and pasted from Designing for Performance:

    Avoid Internal Getters/Setters

    In native languages like C++ it’s
    common practice to use getters (e.g. i
    = getCount()) instead of accessing the field directly (i = mCount). This is
    an excellent habit for C++, because
    the compiler can usually inline the
    access, and if you need to restrict or
    debug field access you can add the
    code at any time.

    On Android, this is a bad idea.
    Virtual method calls are expensive,
    much more so than instance field
    lookups. It’s reasonable to follow
    common object-oriented programming
    practices and have getters and setters
    in the public interface, but within a
    class you should always access fields
    directly.

    Without a JIT, direct field access is
    about 3x faster than invoking a
    trivial getter. With the JIT (where
    direct field access is as cheap as
    accessing a local), direct field
    access is about 7x faster than
    invoking a trivial getter. This is
    true in Froyo, but will improve in the
    future when the JIT inlines getter
    methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this might be a no-brainer, but please read on. I also know
(Sorry if this sounds like a rant, but it's a real question and I'd
StyleCop has a rule about using this. prefix to calling class members (SA1101). Is
I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I
I know the rule is to NEVER throw one during a destructor, and I
Know of any good libraries for this? I did some searches and didn't come
I know, I would have thought the answer was obviously no as well, but
Say I have a method signature like this: protected override void Initialize(params object[] parameters)
I have setup htaccess but i want increase the length of the rule so
Isn't this sort of a thumb-rule? Shouldn't this be served from a cookie-less domain?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.