• 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
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Are Instance variables private by default?

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a bunch!!
 
Sheriff
Posts: 67581
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Beware the other head of science - it bites! Nibble on this message:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic