This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt about private modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt about private modifier" Watch "Doubt about private modifier" New topic
Author

Doubt about private modifier

Marianne Rivera
Greenhorn

Joined: May 05, 2009
Posts: 5
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

Joined: Mar 07, 2009
Posts: 120
Since this main method is inside the same class it can access its private variables (and methods).
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3899
    
    9

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Marianne Rivera
Greenhorn

Joined: May 05, 2009
Posts: 5
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.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Doubt about private modifier
 
It's not a secret anymore!