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 7443809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:23:30+00:00 2026-05-29T11:23:30+00:00

I can call variables 2 ways. One is just to do it like this:

  • 0

I can call variables 2 ways.

One is just to do it like this:

MyClass myClass = new MyClass();    

myLocalVar = myClass.myVarVal;

And the other way is to use a getter like this:

myLocalVar = myClass.getMyVarVal();

Both ways are working fine, but I was wondering what would be the most efficient/proper way of doing this?

Thanks

  • 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-29T11:23:31+00:00Added an answer on May 29, 2026 at 11:23 am

    Both techniques are terrible, but using the getter is the common (and safer) practice.

    In order to access a public data member (a.k.a. public field or public property) of a class, you must know the implementation details of the class (the data member name and the data member type). This is a bad thing; it breaks the OOP concept “information hiding” and increases “coupling”.

    Using a getter is also bad (as in a bad OOP practice) because objects are not just wrappers around data; objects are supposed to encapsulate functionality and data. “store this here value so I can get it later” is not functionality; it is hoot functionality (as in a monkey in a cage hooting). Getters are; however, an accepted practice in java (and other OOP-lite languages like c++ and c#).

    Lest you think I am some ivory tower purest, of course I use getters; I use java, so I use getters.

    Getters are fine for getting the work done (no pun), just don’t walk around believing that “I R gud OOP Prgmr”, because if you use getters you are not a “good oop programmer”, you are just a programmer who gets work done.

    Edit: Perhaps a better way.

    The better way is to not use getters, but to instead design your classes so they expose functionality not data. In practice, there is a point where this breaks down; for example, if you need to display an address on a JSP page, you put a bean in the request (or session or blah) with the address and expose the values using getters. A “more oop pure” way would be to put a bean that exposed “display the address on a jsp” functionality.

    Edit2: Perhaps a better example.

    Say I work for a phone company, in the USA, and I have an object that represents a customers phone number. This might look like the following:

    public class CustomerPhoneNumber
    {
      private String npa; // numbering plan area (google search nanp for more details)
      private String nxx; // exchange.
      private String serviceNumber;
    
      public String toString()
      {
        return "(" + npa + ") " + nxx + "-" + serviceNumber;
      }
    
      public boolean equals(Object object)
      {
        ... standard equals implementation (assume this works)
      }
    }
    

    Now say I get a phone number as an input from a web page in the form String inputPhoneNumber. For the purposes of discussion, the class that receives this input is called “the servlet”.

    How can I answer this question: “Is the input phone number in my list of CustomerPhoneNumber objects?”

    Option 1 is make the npa, nxx, and serviceNumber data members public and access them. This is terrible.

    Option 2 is provide getters for npa, nxx, and service number and compare them with the input. Also terrible, too many internal details exposed.

    Option 3 is provide a getter that returns the formatted phone number (I called this toString() above). This is smarter but still terrible because the servlet has to know the format that will be used by the getter and ensure that the input is formatted the same way.

    Option 4 (I call this “Welcome to OOP”) provide a method that takes a String and returns true if that matches the customer service number. This is better and might look like this (the name is long, but sufficient for this example):

    public boolean doesPhoneNumberMatchThisInput(final String input)
    {
       String formattedInput;
       String formattedCustomerPhoneNumber = npa + nxx + serviceNumber;
    
       formattedInput = ... strip all non-digits from input.
    
       return StringUtils.equals(formattedCustomerPhoneNumber, formattedInput);
    }
    

    This is the winner because no implementation details are exposed. Also the toString can be used to output the phone number on a JSP page.

    StringUtils is part of Apache Commons Lang.

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

Sidebar

Related Questions

I know I can call it whatever, but just like there is a convention
var johnson = button.addEventListener('click', function() { }); In what ways i can use this
From this code I can call bmwCars.CopyToDataTable() as I expected. var bmwCars = from
I can call the webservie directly to the browser with the following URL and
I know I can call the GetVersionEx Win32 API function to retrieve the Windows
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I understand that I can call ToString().IndexOf(...), but I don't want to create an
Using a delegate I can call any function asynchronously. From the documentation I understand
(Yes I know I can call Java code from Scala; but that is pointless;
How exactly using VB6 can I can call any Windows shell command as you

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.