The correct way to get a variable from another class

I can call variables in two ways.

One of them should do it as follows:

MyClass myClass = new MyClass(); myLocalVar = myClass.myVarVal; 

And another way is to use a getter as follows:

 myLocalVar = myClass.getMyVarVal(); 

Both methods work fine, but I was wondering what would be the most efficient / correct way to do this?

thanks

+6
source share
6 answers

Both methods are terrible, but using getters is a common (and safer) practice.

In order to access a public data element (public or public property aka) of a class, you must know the information about the implementation of this class (name of the data member and type of data element). This is bad. it violates the OOP concept of "information hiding" and increases the "communication".

Using a getter is also bad (as in bad OOP practice), because objects are not just wrappers around data; objects must encapsulate functionality and data. “Save this value here so that I can get it later” - this is not functionality; this is the hoot function (like a monkey in a hage hack). Getters; however, accepted practice in java (and other OOP-lite languages ​​such as C ++ and C #).

So that you do not think that I am the bluest ivory tower, I, of course, use getters; I use java, so I use getters.

Getters are great for doing the job (no puns), just don't go around believing that "IR gud OOP Prgmr" because if you use getters you are not a "good programmer", you are just a programmer who does the work.

Edit: Perhaps the best way.

It’s best not to use getters, but instead create your own classes so that they display functionality, not data. In practice, there is a moment when it breaks; for example, if you need to display the address on the JSP page, you put a bean in the request (or session or blah) with the address and set the values ​​using getters. A "cleaner clean" way would be to put a bean that exposed "address mapping in jsp".

Edit2: Perhaps the best example.

Let's say I work for a telephone company in the USA, and I have an object that is a phone number for customers. It might look like this:

 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 that I get the phone number as input from a web page in the form of String inputPhoneNumber . For discussion purposes, the class that receives this input is called a "servlet."

How can I answer this question: "Is the entered phone number in my list of CustomerPhoneNumber objects?"

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

Option 2 provides getters for npa, nxx and a service number and compares them with input. Also terrible, too many internal parts are exposed.

Option 3 is a getter that returns a formatted phone number (I called it toString () above). This is smarter, but still terrible, because the servlet needs to know the format that the recipient will use and ensure that the input is formatted in the same way.

Option 4 (I call it "Welcome to OOP") provides a method that takes a string and returns true if it matches the client service number. This is better and might look like this (the name is long, but enough 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 a winner because implementation details are not displayed. Also toString can be used to display the phone number on the JSP page.

StringUtils is part of the Apache Commons Lang .

+11
source

For encapsulation, you should always go with a second alternative.

 myLocalVar = myClass.getMyVarVal(); 

Efficiency is reasonable, you most likely will not notice the difference.

+8
source

ALWAYS use getter and setter to access your properties!

You should also take a look at.

+4
source

Just create an object and object.variablename; or object.methodName(); can be used to create a non-static link ... using a getter is not required.

+2
source

myClass.getMyVarVal() is slower as it is a method call and therefore it creates an entry on the stack for the return value, etc. But it is better to use OOP to use getters.

+1
source
 myLocalVar = myClass.getMyVarVal(); 

it will be useful to use it if you are working with the concept of OOP

+1
source

Source: https://habr.com/ru/post/908045/


All Articles