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

  • SEARCH
  • Home
  • 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 9295963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:43:52+00:00 2026-06-18T21:43:52+00:00

I am studying programming and although I feel like I have good knowledge in

  • 0

I am studying programming and although I feel like I have good knowledge in programming (been writing PHP, Javascript etc for years), I’ve never dived into the world of OOP. Because of this I am asking for a general answer for the situation I am in. I know that there always will be some exceptions, but I am looking for the most correct approach in this case.

Ok, so I have two classes, Game and GameGraphics. GameGraphics will create an object of Game and use all the methods it has. We recently learned that all variables a class should be private, so all the variables in Game are accessed by GameGraphics through getters and setters.

But, when I want to access the variables of Game inside the Game class, should I use the public methods, or just access the variables directly? What is the most correct approach to this?

I’m developing in Java, if that matters. And please excuse my lack of OOP-experience.

  • 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-06-18T21:43:53+00:00Added an answer on June 18, 2026 at 9:43 pm

    The two major reasons behind using accessor methods (getters and setters) to access variables of other classes are

    • An ability to present accessors as members of an interface, and
    • An ability to override an accessor in a subclass.

    A slight drop in readability is a reasonable price to pay for these advantages, because they let you encapsulate the logic of your class, resulting in tighter control on your part.

    Neither of these advantages applies when it comes to private methods: they cannot implement interface methods, and they cannot be overridden. In fact, Java compiler would often optimize them out altogether, especially the simpler ones.

    That is why I would recommend going straight for the instance variables, rather than adding an extra layer that does not help readability, yet gets optimized out by the compiler.

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

Sidebar

Related Questions

I have been programming for a couple of years and am now studying computer-science
I have been programming in python exclusively for 4 years and have never really
I'm studying programming on my own and I would like to have an idea
i have been studying about cuda. In CUDA Programming guide, shared memory access time
I'm a newbie to programming and know only procedural PHP. I've been studying prepared
I just started studying programming about 6 months ago and I have really been
I am really new to the programming but I am studying it. I have
I'm studying programming and as part of my project I have to use the
I am a student studying software development, and I feel programming, in general, is
I am currently studying programming and I have the following problem. A user should

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.