What is the difference between field, variable, attribute, and property in Java POJOs?


Solution 1

From here: http://docs.oracle.com/javase/tutorial/information/glossary.html

  • field

    • A data member of a class. Unless specified otherwise, a field is not static.

  • property

    • Characteristics of an object that users can set, such as the color of a window.

  • attribute

    • Not listed in the above glossary

  • variable

    • An item of data named by an identifier. Each variable has a type, such as int or Object, and a scope. See also class variable, instance variable, local variable.

Solution 2

Yes, there is.

Variable can be local, field, or constant (although this is technically wrong). It's vague like attribute. Also, you should know that some people like to call final non-static (local or instance) variables

"Values". This probably comes from emerging JVM FP languages like Scala.

Field is generally a private variable on an instance class. It does not mean there is a getter and a setter.

Attribute is a vague term. It can easily be confused with XML or Java Naming API. Try to avoid using that term.

Property is the getter and setter combination.

Some examples below

public class Variables {

    public final static String MY_VARIABLE = "that was a lot for a constant";

    final String dontChangeMeBro = "my god that is still long for a val";

    protected String flipMe = "wee!!!";

    private String ifYouThoughtTheConstantWasVerboseHaHa;

    //Still the property
    public String getIfYouThoughtTheConstantWasVerboseHaHa() {
        return ifYouThoughtTheConstantWasVerboseHaHa;

    //And now the setter
    public void setIfYouThoughtTheConstantWasVerboseHaHa(String ifYouThoughtTheConstantWasVerboseHaHa) {
        this.ifYouThoughtTheConstantWasVerboseHaHa = ifYouThoughtTheConstantWasVerboseHaHa;


There are many more combinations, but my fingers are getting tired :)

Solution 3

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"/>

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"/>

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.

Solution 4

If your question was prompted by using JAXB and wanting to choose the correct XMLAccessType, I had the same question. The JAXB Javadoc says that a "field" is a non-static, non-transient instance variable. A "property" has a getter/setter pair (so it should be a private variable). A "public member" is public, and therefore is probably a constant. Also in JAXB, an "attribute" refers to part of an XML element, as in <myElement myAttribute="first">hello world</myElement>.

It seems that a Java "property," in general, can be defined as a field with at least a getter or some other public method that allows you to get its value. Some people also say that a property needs to have a setter. For definitions like this, context is everything.

Solution 5

Dietel and Dietel have a nice way of explaining fields vs variables.

“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)


Related videos on Youtube

Victor Lyuboslavsky
Author by

Victor Lyuboslavsky

Enthusiast programmer, entrepreneur, author. My Book Telemedicine and Telehealth 2.0 Favorite Resources EDA Playground - run HDL simulations online (supports SystemVerilog, Verilog, VHDL, and other HDLs) SystemVerilog LRM IEEE Std 1800-2012 (includes legacy Verilog) UVM 1.2 Class Reference UVM 1.1d Class Reference

Updated on June 15, 2022


  • Victor Lyuboslavsky
    Victor Lyuboslavsky less than a minute

    When referring to internal private variables of Java POJOs that have getters/setters, I've used the following terms:

    • field
    • variable
    • attribute
    • property

    Is there any difference between the above? If so, what is the correct term to use? Is there a different term to use when this entity is persisted?

  • Adam Gent
    Adam Gent about 10 years
    @Chris Thompson Thats what Eclipse calls it in one of its dialog. Hey folks its Java. What can I tell you. It doesn't make sense.
  • emory
    emory about 10 years
    @AdamGent JAVA=Just Another Vague Acronym :)
  • Voo
    Voo about 10 years
    @emory Well it's the old well known oxymoron. That's nothing specific to Java, that "problem" goes back till at least C and probably longer.. obviously nothing stops you from calling it just "constant"
  • Chris Thompson
    Chris Thompson about 10 years
    @AdamGent Wasn't serious, just giving you a hard time ;-)
  • NateDSaint
    NateDSaint over 8 years
    I saw the @ names and totally thought they were annotation jokes.
  • Koray Tugay
    Koray Tugay over 6 years
    I still do not understand the difference between a field and a property?
  • jahroy
    jahroy over 6 years
    @KorayTugay - Look at a field as a rudimentary piece of data related to an object. A property (to me) is a characteristic of an object that is visible (and apparently mutable) to the outside world.
  • jdurston
    jdurston over 6 years
    Based on the above, would it be fair to say "Properties and fields are the same, except a property is a settable field"?
  • jahroy
    jahroy over 6 years
    @John - Maybe based on the above, which, as noted, is a copy/paste from Oracle. Personally I'd make the distinction that a property is publicly visible and possibly mutable, whereas a field could be an internal, private field used only by the class.
  • sargas
    sargas about 6 years
    Good explanation while making fun of how identifier names can get ridiculously long (and they DO get ridiculously long often).
  • Koray Tugay
    Koray Tugay about 6 years
    How about a property?
  • Kartik Chugh
    Kartik Chugh over 5 years
    I think the best thing to stress is that existence as a field and a property is not mutually exclusive. From the way this answer is worded, to new developers, it looks like a list of distinct things.
  • kaushik
    kaushik almost 2 years
    @jahroy, Is it safe to say that a field can be property when it is public and also not final ? or can we say that a field with a getter and setter is a property ?
  • evaldeslacasa
    evaldeslacasa over 1 year
    I didn't like the "field" definition. Although, in the glossary, "instance variable" is defined as: *Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field. * I like that one better.
  • simon
    simon over 1 year
    @KorayTugay A property is also a field, but a field is not necessary a property. Property is a hyponym of field (and field is a hypernym of property). A field is also a property, when its value can be set (typically with a setter method).