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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Are Instance variables private by default?" Watch "Are Instance variables private by default?" New topic
Author

Are Instance variables private by default?

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
I just made a class with some string variables(such as name, color etc.) and have been able to access them and set their values by making an object of that class from another (tester)class in its main. I thought instance variables were private by default when no access specifier preceeds them. I thought when you want to access such variables you have to go thru public setter methods.
what do I do now?
Mattias Arthursson
Ranch Hand

Joined: Jul 26, 2004
Posts: 90
If you want your instance variables to be private (which is indeed appropriate) you need to declare them as private. The default access modifier (if not private, protected or public is specified) is package private, which means that it may be accessed from any other class in the same package as the declaring class.


Admit nothing. Blame everyone. Be bitter.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Thanx a bunch!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

"AmitB.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mattias Arthursson:
... The default access modifier (if not private, protected or public is specified) is package private, which means that it may be accessed from any other class in the same package as the declaring class.

Note that this default has no official name. It's sometimes just called "package" or "friendly."

Also note that if no package is explicitly declared in your file, then it is part of the "default package," which is the directory.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

The default access modifier (if not private, protected or public is specified) is package private, which means that it may be accessed from any other class in the same package as the declaring class.

This is not true. The default access modifier is context dependent.
- The default access modifier of interface methods is public
- The default access modifier of interface fields is public
- The default access modifier of enum constructors is private
- etc. etc.


Tony Morris
Java Q&A (FAQ, Trivia)
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
as my java guru bruce eckels and K&B says "java is not c++" repeat it for five times. try to know about all the four access specifiers viz., public, protected, private and friendly. it is very important in java.


Regards,
vignesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Instance variables private by default?