Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public or private access?

 
Jakub Drzazga
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have written a code:
public class SubcontractorRecord implements Serializable{

private static final long serialVersionUID = 4356425461146655727L;

public String name;
public String location;
public String specialties;
public int size;
public String rate;
public String owner;

public SubcontractorRecord(String name, String location, String specialties, int size, String rate, String owner){
this.name=name;
this.location=location;
this.specialties=specialties;
this.size=size;
this.rate=rate;
this.owner=owner;
}
}

Can I leave it like this? With getters and setters this code would be a little messy.

It is written in Java code conventions:

One example of appropriate public instance variables is the case where the class is essentially a data structure, with no behavior. In other words, if you would have used a struct instead of a class (if Java supported struct), then it's appropriate to make the class's instance variables public.
What is your opinion about that?
 
Alecsandru Cocarla
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some problems that I see with this approach is if you later want to add some behavior, like for example:
- making this object "thread-safe"
- add some checks for nulls or empty strings in setters
- add some other checks (like, for example, isOwned() - which might look odd besides all those public fields)
Another disadvantage - nobody can use your class as a JavaBean (property listeners and stuff - see, for example, http://www.unix.com.ua/orelly/java-ent/jnut/ch06_02.htm for some specifications).

My personal opinion - I would not leave it like this.
If you add accessors, nobody will complain that your code is messy. It just follows the normal bean conventions (you should, however, add a default constructor to really follow the convention).
If you don't add them, some people might think it's strange.

Although, it's true, I've also met people which really hate accessors, and they also had some arguments for this.
 
Kah Tang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making your instance variables public is a big no no in OO design. The whole point of using getters and setters is that when any business rules changes for that class in future versions, you can't change it in your class anymore. Changing it would break other people's code when they depend on that class.

Leaving it like you would now would definitely mean major point loss.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I leave it like this?


This way, you're violating one of the object-oriented fundamentals: encapsulation. If you leave like this, you'll certainly lose marks.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use only private modifier unless you have a good reason to make it public.

Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2) --> Waiting for the result
 
Alecsandru Cocarla
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I wouldn't say that this is really that dramatic. I don't think you'll necessarily lose points for this decision, considering that your class is in fact some kind of DTO which usually should not have any behavior. But, anyway, to be safe, you'd better use accessors.
 
Jethro Borsje
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I feel this approach is a no-go. Making fields public could lead to all kinds of problem (thread safety, no way to validate the values, etc.). However, if you feel like taking the risk than document your decision.
 
Jakub Drzazga
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys I think you�re right this code is to risky for SCJD.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys I think you�re right this code is too risky for SCJD.


It is even more risky if you do it in the real world. You'll become very unpopular quickly. .

And I can't imagine how angry your boss

Good luck on your assignment and wish me luck too !!!

Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2) --> Waiting for the result
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic