Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are Instance variables private by default?

 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a bunch!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic