• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

public or private access?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Legend has it that if you rub the right tiny ad, a genie comes out.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic