Java POJO에서 필드, 변수, 속성 및 속성의 차이점은 무엇입니까?
getter / setter가있는 Java POJO의 내부 개인 변수를 언급 할 때 다음 용어를 사용했습니다.
- 들
- 변하기 쉬운
- 속성
- 특성
위의 차이점이 있습니까? 그렇다면 올바른 용어는 무엇입니까? 이 엔티티가 지속될 때 사용할 다른 용어가 있습니까?
여기에서 : http://docs.oracle.com/javase/tutorial/information/glossary.html
들
- 클래스의 데이터 멤버 달리 지정하지 않으면 필드는 정적이 아닙니다.
특성
- 창의 색상과 같이 사용자가 설정할 수있는 객체의 특성.
속성
- 위의 용어집에 나열되지 않음
변하기 쉬운
- 식별자로 명명 된 데이터 항목. 각 변수에는 int 또는 Object와 같은 유형과 범위가 있습니다. 클래스 변수, 인스턴스 변수, 로컬 변수를 참조하십시오.
그렇습니다.
변수 는 로컬, 필드 또는 상수 일 수 있습니다 (기술적으로 잘못 되었더라도). 애매 모호한 속성입니다. 또한 일부 사람들은 정적이 아닌 최종 (로컬 또는 인스턴스) 변수 를 호출하는 것을 좋아 합니다.
" 값 ". 이것은 아마도 Scala와 같은 새로운 JVM FP 언어에서 비롯된 것입니다.
필드 는 일반적으로 인스턴스 클래스의 전용 변수입니다. 게터와 세터가 있다는 의미는 아닙니다.
속성 은 모호한 용어입니다. XML 또는 Java Naming API와 쉽게 혼동 될 수 있습니다. 해당 용어를 사용하지 마십시오.
속성 은 게터와 세터 조합입니다.
아래 몇 가지 예
public class Variables {
//Constant
public final static String MY_VARIABLE = "that was a lot for a constant";
//Value
final String dontChangeMeBro = "my god that is still long for a val";
//Field
protected String flipMe = "wee!!!";
//Property
private String ifYouThoughtTheConstantWasVerboseHaHa;
//Still the property
public String getIfYouThoughtTheConstantWasVerboseHaHa() {
return ifYouThoughtTheConstantWasVerboseHaHa;
}
//And now the setter
public void setIfYouThoughtTheConstantWasVerboseHaHa(String ifYouThoughtTheConstantWasVerboseHaHa) {
this.ifYouThoughtTheConstantWasVerboseHaHa = ifYouThoughtTheConstantWasVerboseHaHa;
}
}
더 많은 조합이 있지만 손가락이 피곤해집니다. :)
JAXB를 사용하여 질문을 받았고 올바른을 선택하고 싶다면 XMLAccessType
같은 질문이 있습니다. JAXB Javadoc에 따르면 "필드"는 비 정적 비 일시적 인스턴스 변수입니다. "속성"에는 게터 / 세터 쌍이 있습니다 (따라서 개인 변수 여야 함). "공개 구성원"은 공개이므로 일정 할 수 있습니다. 또한 JAXB에서 "속성"은에서와 같이 XML 요소의 일부를 나타냅니다 <myElement myAttribute="first">hello world</myElement>
.
일반적으로 Java "속성"은 최소한 getter 또는 값을 얻을 수있는 다른 공용 메소드가있는 필드로 정의 될 수 있습니다. 어떤 사람들은 또한 재산에 세터가 필요하다고 말합니다. 이와 같은 정의의 경우 컨텍스트가 전부입니다.
Dietel과 Dietel은 필드 대 변수를 설명하는 좋은 방법이 있습니다.
“Together a class’s static variables and instance variables are known as its fields.” (Section 6.3)
“Variables should be declared as fields only if they’re required for use in more than one method of the class or if the program should save their values between calls to the class’s methods.” (Section 6.4)
So a class's fields are its static or instance variables - i.e. declared with class scope.
Reference - Dietel P., Dietel, H. - Java™ How To Program (Early Objects), Tenth Edition (2014)
If you take clue from Hibernate:
Hibernate reads/writes Object's state with its field. Hibernate also maps the Java Bean style properties to DB Schema. Hibernate Access the fields for loading/saving the object. If the mapping is done by property, hibernate uses the getter and setter.
It is the Encapsulation that differentiates means where you have getter/setters for a field and it is called property, withthat and we hide the underlying data structure of that property within setMethod, we can prevent unwanted change inside setters. All what encapsulation stands for...
Fields must be declared and initialized before they are used. Mostly for class internal use.
Properties can be changed by setter and they are exposed by getters. Here field price has getter/setters so it is property.
class Car{
private double price;
public double getPrice() {…};
private void setPrice(double newPrice) {…};
}
<class name="Car" …>
<property name="price" column="PRICE"/>
</class>
Similarly using fields, [In hibernate it is the recommended way to MAP using fields, where private int id; is annotated @Id, but with Property you have more control]
class Car{
private double price;
}
<class name="Car">
<property name=" price" column="PRICE" access="field"/>
</class>
Java doc says: Field is a data member of a class. A field is non static, non-transient instance variable. Field is generally a private variable on an instance class.
Variables are comprised of fields and non-fields.
Fields can be either:
- Static fields or
- non-static fields also called instantiations e.g. x = F()
Non-fields can be either:
- local variables, the analog of fields but inside a methods rather than outside all of them, or
- parameters e.g. y in x = f(y)
In conclusion, the key distinction between variables is whether they are fields or non-fields, meaning whether they are inside a methods or outside all methods.
Basic Example (excuse me for my syntax, I am just a beginner)
Class {
//fields
method1 {
//non-fields
}
}
Actually these two terms are often used to represent same thing, but there are some exceptional situations. A field can store the state of an object. Also all fields are variables. So it is clear that there can be variables which are not fields. So looking into the 4 type of variables (class variable, instance variable, local variable and parameter variable) we can see that class variables and instance variables can affect the state of an object. In other words if a class or instance variable changes,the state of object changes. So we can say that class variables and instance variables are fields while local variables and parameter variables are not.
If you want to understand more deeply, you can head over to the source below:-
http://sajupauledayan.com/java/fields-vs-variables-in-java
The question is old but another important difference between a variable and a field is that a field gets a default value when it's declared.A variable, on the other hand, must be initialized.
My understanding is as below, and I am not saying that this is 100% correct, I might as well be mistaken..
A variable is something that you declare, which can by default change and have different values, but that can also be explicitly said to be final. In Java that would be:
public class Variables {
List<Object> listVariable; // declared but not assigned
final int aFinalVariableExample = 5; // declared, assigned and said to be final!
Integer foo(List<Object> someOtherObjectListVariable) {
// declare..
Object iAmAlsoAVariable;
// assign a value..
iAmAlsoAVariable = 5;
// change its value..
iAmAlsoAVariable = 8;
someOtherObjectListVariable.add(iAmAlsoAVariable);
return new Integer();
}
}
So basically, a variable is anything that is declared and can hold values. Method foo above returns a variable for example.. It returns a variable of type Integer which holds the memory address of the new Integer(); Everything else you see above are also variables, listVariable, aFinalVariableExample and explained here:
A field is a variable where scope is more clear (or concrete). The variable returning from method foo 's scope is not clear in the example above, so I would not call it a field. On the other hand, iAmAlsoVariable is a "local" field, limited by the scope of the method foo, and listVariable is an "instance" field where the scope of the field (variable) is limited by the objects scope.
A property is a field that can be accessed / mutated. Any field that exposes a getter / setter is a property.
I do not know about attribute and I would also like to repeat that this is my understanding of what variables, fields and properties are.
'Programing' 카테고리의 다른 글
phantomjs가 "전체"페이지로드를 기다리지 않습니다 (0) | 2020.06.24 |
---|---|
프로그래밍 방식으로 APK 설치 / 제거 (PackageManager 및 의도) (0) | 2020.06.24 |
PowerShell에서 변수에 저장된 명령 실행 (0) | 2020.06.24 |
Python-파일 대 사용시기 (0) | 2020.06.24 |
C ++ 11에서 값별 전달이 합리적인 기본값입니까? (0) | 2020.06.24 |