• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private keyword

 
christopher pam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giving a situation where we have;

public class GradeBook
{
private String courseName;

public void setCourseName(String name)
{
--------------------------;
}

courseName being a private instance variable should only be accessed and manipulated by methods of thesame class,like setCourseName.wouldnt it be possible to access it(courseName)with a method of another class that can call the method setCourseName?if this is possible then i think the whole purpose of the private keyword which is data hiding will be defeated.could really use some help concerning this,thanks
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi christopher, welcome to Javaranch family.

First of all, you must understand that OO principles dont include data hiding, its rather data encapsulation. It means giving access to data after proper validation. By marking the instance variable public, you risk data leakage and its reckless modification. E.g, someone might set an instance variable as negative, when it should never be negative.

Thus the main aim of marking the instance variables private and then using getters and setters to access them is validation along with access, so, for example one cannot directly set a value negative for a variable int weight;.

And if one doesn't want private variables to be accessed by other classes, no accessor classes should be provided.

Enjoy!!!
[ March 14, 2007: Message edited by: Sidd Cool ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sidd Cool is quite right, but there might be a better way of prohibiting access to private fields: make the accessor method private too
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is that if you have a setCourseName(...), then there you could check for example if the name passed to the method is a valid course name. If you make the courseName member variable public, then the class has no control at all; any other class can modify it to any value it likes, including invalid values.

Campbell Ritchie: "...make the accessor method private too"

In my opinion it makes no sense to have private getter and setter methods. Inside your class, just use the member variable directly. Accessing your own private member variables via private getters and setters is overkill. The class should know what it's doing and should know what restrictions apply to its own member variables, so there should be no need to access them only via getters and setters.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some very cool classes have private data and few or no getters or setters. Look at String. Once you make one you can't set or change anything about it. You can get some stuff, bytes, chars, length, but you always get copies of those things - you can't change them and cause the String to change.

Adding public getters and setters to private variables is something we debate now and then. It makes them as good as public, but it gives you a way to control how things are changed. You could even change the internal storage without telling anyone who uses the getters and setters. Another classic example is a getAge() which actually computes from dateOfBirth and has no variable. So they can be useful.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wouldnt it be possible to access it(courseName)with a method of another class that can call the method setCourseName?

The idea is that nobody can DIRECTLY access your instance variable. they HAVE to go through your method. This allows you to control what they can and can't do.

it also lets you change your String to anything else that you want, so long as you then change your setter method. the other class can still pass you a string, and you can do whatever mapping you want to your new internal representation.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it not also useful in multi thread application where you can synchronize the getter and setter and not the variable thus ensuring that one thread dosnt change the value while another thread is working with that variable.

G
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic