9

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

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • 3
    Lots of resources [here](http://stackoverflow.com/q/1568091/387852), [here](http://stackoverflow.com/q/565095/387852), and [here](http://stackoverflow.com/q/996179/387852) for instance. – Michael McGowan Feb 09 '12 at 14:46
  • But what if I have like 50 variables that describe an object. For example, lets say I have an object called Car, and then I have a bunch of variables like carType, carColor, carHP, carMaker, carName, carWheelBase, carMPG, carTransmission, carEngine, etc...do I need to make a getter and setter for each one? That seems like a TON of extra coding.... – SkyeBoniwell Feb 09 '12 at 14:58
  • 2
    Modern IDEs (like Eclipse) can do that automatically. But you should also ask yourself if you need to expose those variables to the outside world. Are you violating good principles of encapsulation? – Michael McGowan Feb 09 '12 at 15:08
  • Well the user needs to know all the properties of the car...So I pull the data from a database or XML file, assign via a setter, and then use a getter to show to the user. At least thats my limited understanding of how it should work. – SkyeBoniwell Feb 09 '12 at 15:10

7 Answers7

16

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.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • so is there a better way or are getters just a necessary evil? – SkyeBoniwell Feb 09 '12 at 15:01
  • There is an [excellent article here](http://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html) for those looking for a more thorough explanation (see the *A Ball and A Dog* section for a very understandable analogy.) – arkon Jul 20 '15 at 08:59
8

For the sake of encapsulation you should always go with the second alternative.

myLocalVar = myClass.getMyVarVal();

Efficiency wise you most likely won't notice a difference.

aioobe
  • 413,195
  • 112
  • 811
  • 826
4

Do ALWAYS use getter and setter to access your properties!

You should also take a look at this.

Community
  • 1
  • 1
oopbase
  • 11,157
  • 12
  • 40
  • 59
2

Just create object and object.variablename; or object.methodName(); can be used to make non-static reference...no use of getter is required.

2

myClass.getMyVarVal() is slower since it is a method call and so it creates entrance on the stack for return value, etc. But it is better OOP practice to use getters.

Asterisk
  • 3,534
  • 2
  • 34
  • 53
  • 2
    The Java language does not dictate that the method call should be implemented like that. In fact, most likely such method will get inlined. – aioobe Feb 09 '12 at 14:46
1
myLocalVar = myClass.getMyVarVal();

it will be good to use it if you are working with OOP concept

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
0

Tomcat + Heroku + Maven project:

How to reference Main class static variable:

HEROKU_PRJ_FOLDER\src\main\java\servlet\HelloServlet.java:

import launch.Main;
String my_str = Main.TEST_STRING;

HEROKU_PRJ_FOLDER\src\main\java\launch\Main.java

package launch;
....other imports here....
public class Main {

    public static final String
    TEST_STRING = "[TEST_STRING]";

    public static void main(String[] args){
        ...somelogic...
    };
};

This will probably work for any Tomcat project, but I did this using Tomcat+Heroku+Maven. Posted answer because the closest question I could find was this, which I already knew how to do, just the exact import paths I found a bit confusing for my particular problem.

KANJICODER
  • 3,611
  • 30
  • 17