Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way of getting variable from another class

Tags:

java

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

like image 790
SkyeBoniwell Avatar asked Feb 09 '12 14:02

SkyeBoniwell


4 Answers

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.

like image 176
DwB Avatar answered Nov 15 '22 15:11

DwB


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.

like image 41
aioobe Avatar answered Nov 15 '22 15:11

aioobe


Do ALWAYS use getter and setter to access your properties!

You should also take a look at this.

like image 26
oopbase Avatar answered Nov 15 '22 15:11

oopbase


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.

like image 33
Asterisk Avatar answered Nov 15 '22 17:11

Asterisk