999cm999 999cm999 - 2 years ago 67
Java Question

Proper way of getting variable from another class

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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download