Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about private modifier

 
Marianne Rivera
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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..
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this main method is inside the same class it can access its private variables (and methods).
 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic