• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about private modifier

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am in doubt in what I am learning.



I thought setting a private access modifier will prevent my instance variable from being set incorrectly.

But when I compile this class using javac, I encounter no error. I thought there will be an error in the head first java book.

I am having a hard time figuring how to learn java.. Please help..
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this main method is inside the same class it can access its private variables (and methods).
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marianne Rivera wrote:I thought setting a private access modifier will prevent my instance variable from being set incorrectly.
But when I compile this class using javac, I encounter no error.



That's allowed only within the same class as the private variable. No one can access it directly outside the class.

I thought there will be an error in the head first java book.
I am having a hard time figuring how to learn java..



You are with the right material. Just work out the things in the book.
 
Marianne Rivera
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

So thats the reason. Now I understand why the book is always talking about the TestDrive Class though I am not yet sure about the keyword static upto now. Maybe later the book will explain it to me.. hopefully..

Thanks to both of you.
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic