The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

fiallega (9) [Avatar] Offline
#1
I am reading the first edition on GroovyBeans 7.4
I would love to see a clear explanation of the following in the second edition when the chapters come out. (and if you can explain it to me now, even better.
In general terms I am confussed on what is a property vs an attribute
I think this code will explain it better


class Employee implements java.io.Serializable {
String name
private ssn
public address
private String zip
}

// implicit getter produced for property
def employee = new Employee()

// this calls the implicit set ... right ?
employee.name = "Juan de Marco"
assert employee.getName() == "Juan de Marco"

// No implicit get/set for ssn why?
try{
assert employee.getSsn() == "333-33-3333"
}catch(MissingMethodException mme){
assert true
}

// so how is this possible if above is true (no implicit getters/setters) and is private?
employee.ssn = "333-33-3333"

// or how is this possible
assert employee.ssn == "333-33-3333"

// I thought default property was private so I dont understand
// why zip and name dont have the same behavior, that is
// zip is "NOT a property" therefore no implicit get/set
employee.zip="33133"
try{
assert employee.getZip() == "33133"
}catch(MissingMethodException mme){
assert true
}

// and what makes something a property vs an attribute ?
// remember class and metaClass are there
assert employee.properties.size() == 3
fiallega (9) [Avatar] Offline
#2
Re: Groovy Beans
so, I think I can answer one one of my questions
Property vs Field
a Property gets created if default accessor is used (no accessor) which in turn will produce implicit getters and setters

I still can not explain in the case of fields
// so how is this possible if above is true (no implicit getters/setters) and is private?
employee.ssn = "333-33-3333"

// or how is this possible
assert employee.ssn == "333-33-3333"
Mittie (397) [Avatar] Offline
#3
Re: Groovy Beans
Hi,

in 7.4.1, "declaring beans" it says "In
Groovy, it is easier to assess what fields are considered exposed properties: all
fields that are declared with default visibility."

Maybe we should make this line stand out more.

Would that help you?

thanks for posting
Dierk