This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt about private modifier  RSS feed

 
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
Posts: 3977
27
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
Boost this thread!