wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes private keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private keyword" Watch "private keyword" New topic
Author

private keyword

christopher pam
Greenhorn

Joined: Jan 12, 2005
Posts: 5
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

Joined: Feb 20, 2007
Posts: 152
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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Aug 16, 2005
Posts: 14279
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11448
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private keyword