[groovy-user] Property vs Field

739 views
Skip to first unread message

Schalk Cronj é

unread,
Nov 21, 2014, 3:45:31 PM11/21/14
to us...@groovy.codehaus.org
In Groovy when referring to a data member of a class such as below

class A {
String name
}

what is the preferred way of referring to 'name' ? Is it a field or a
property?

--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Michał Mally

unread,
Nov 21, 2014, 3:53:41 PM11/21/14
to us...@groovy.codehaus.org
Hi Schalk,

Defining class as below is equivalent to below construction in Java:

public class A {

private String name

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}

Basically in Groovy if you omit access modifier when field is
concerned:
a) the field is made private
b) getter and setter is automatically generated

Additionally if you refer to field like this:
new A().name
it is just a shortcut to:
new A().getName()

To refer to field directly (not by getter) you can do sth like this:
new A().@name
however it is not recommended.

You can find more on the subject here:
http://groovy.codehaus.org/Groovy+Beans

Best regards,
Michal

W dniu 21.11.2014 o 21:43, Schalk Cronj é pisze:
> In Groovy when referring to a data member of a class such as below
>
> class A {
> String name
> }
>
> what is the preferred way of referring to 'name' ? Is it a field or a
> property?
>


Schalk Cronj é

unread,
Nov 21, 2014, 3:58:08 PM11/21/14
to us...@groovy.codehaus.org
Thanks, but that was not actually the answer to the question. I was
referring to semantics of the property and field words. Although reading
between the lines of your response, you obviously prefer using field
rather than property.
--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r


Cédric Champeau

unread,
Nov 21, 2014, 4:09:20 PM11/21/14
to us...@groovy.codehaus.org
It's a property. Anything which has a modifier is a field. No modifier == property.

Best regards,

chris...@wellsfargo.com

unread,
Nov 21, 2014, 4:12:57 PM11/21/14
to us...@groovy.codehaus.org
>> Is it a field or a property?

The Groovy documentation refers to that as a property, and that is the terminology we try to use in CodeNarc as well.
e.g.
http://groovy.codehaus.org/Groovy+Beans

Chris

Schalk Cronj é

unread,
Nov 21, 2014, 4:16:39 PM11/21/14
to us...@groovy.codehaus.org
Cedric,

Is this what you are saying?

class A {
  String thisIsAProperty
}

class B {
  String thisIsAField
 
  void setThisIsAField( String txt ) { thisIsAField = txt }  // Explicit modifier
}

-- 
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r

Cédric Champeau

unread,
Nov 21, 2014, 4:19:38 PM11/21/14
to us...@groovy.codehaus.org
Hi,

No, I am saying that this:

class A {
   String someProperty
   private String someField1
   protected String someField2
   public String someField3
}

Schalk Cronj é

unread,
Nov 21, 2014, 4:32:08 PM11/21/14
to us...@groovy.codehaus.org
Thanks. I get it now and gives me a baseline to mention the following from Gradle. (I am using the Gradle Jar task as an example).

 In the API docs

    http://gradle.org/docs/current/groovydoc/org/gradle/api/tasks/bundling/Jar.html

it would refer to 'getManifest' as a method, which is correct. However in the DSL docs

    http://gradle.org/docs/current/dsl/org.gradle.api.tasks.bundling.Jar.html

it would refer to 'manifest' as a property. Looking in the source code itself

    https://github.com/gradle/gradle/blob/master/subprojects/platform-jvm/src/main/java/org/gradle/api/tasks/bundling/Jar.java

it is a field (according to Cedric's definition).

Maybe I'm just pedantic, but I would think that is actually a source of confusion. (It is actually the origin of wht I asked the question in the first place).

Cédric Champeau

unread,
Nov 21, 2014, 4:37:32 PM11/21/14
to us...@groovy.codehaus.org
To get things a bit more complicated, anything which has a getter or a setter is in practice a property in Groovy:

class A {
   String getSomeProperty() { ... }
   void setSomeProperty(String p) { ... }

   String getSomeReadOnlyProperty() { ... }

   void setSomeWriteOnlyProperty() { ... }
}

All of them can be accessed through the property notation:

a.someProperty
a.someReadOnlyProperty
a.someWriteOnlyProperty = 'foo'

A field is really what a field is in Java. A property is more general, and can, but not necessarily, be backed by a field.

OC

unread,
Nov 22, 2014, 1:25:33 PM11/22/14
to us...@groovy.codehaus.org
Also, when playing with fields and properties and speaking of confusion, be wary of this gotcha -- can bit rather hard in tender parts: http://groovy.329449.n5.nabble.com/field-preferred-to-getter-Shouldn-t-it-be-the-other-way-td5713987.html

All the best,
OC

Russel Winder

unread,
Nov 23, 2014, 7:54:28 AM11/23/14
to us...@groovy.codehaus.org
On Fri, 2014-11-21 at 13:43 -0700, Schalk Cronj é wrote:
> In Groovy when referring to a data member of a class such as below
>
> class A {
> String name
> }
>
> what is the preferred way of referring to 'name' ? Is it a field or a
> property?

I think I prefer the term evil – it's public mutable state. I think the
term property got coined to try and make it sound acceptable and not
evil.

Of course the whole Bean Protocol (from which all this getter, setter,
accessor, property stuff emanates) is a way of turning an
object-oriented system into evil mess.

--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel...@ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
signature.asc
Reply all
Reply to author
Forward
0 new messages