• Post Reply Bookmark Topic Watch Topic
  • New Topic

private v.s public final  RSS feed

 
stevesean chen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
I am not clear the difference between private and public final. If making filed private prevents outsider from modifying the content of it, then public final should do the same, isn't it? the only difference I can see is private also prevent you from directly accessing it. Would you please explain why one should use private over public final in general or for whatever purpose. Thanks in advance, I am new to java, a new beginner.

I think I am referring to object, rather than primitives.
 
Dan Craciun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you make a field private you cannot access it outside its class.
if you make it final but not private, you can access it outside the class but you cannot change its value (assuming the field its a primitive type).
lets say for example in a file you have a class X with a final with access public/package/protected field A and a private final field B. in another class in the same
file you can access A but you cannot access B

in the case of methods is more or less the same with some interesting results regarding inheritance however : any private methods in a class are implicitly final. Because you can’t access a private method, you can’t override it. You can add the final specifier to a private method, but it
doesn’t give that method any extra meaning. here however are some interesting results regarding inheritance. If you're interested in details let me know.
 
stevesean chen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the detail explanation, Dan Craciun. Really appreciate your help. So according to my understanding, private makes fields completely hidden away from outside of current class. Public allowed the access, and adding a final just prevent the fields from being modified outside of the current class. So if in case that I would like to allow access to the field but not modifying it, can I just declare it as public final instead if private. And my other concern is, if I make it public final, someone from outside of this class, may make a copy of the field, a reference copy, and use it to modify the content of the original field object? Once again thanks for the info!
 
mohana krishna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final HashMap finalHm = new HahMap();

finalHm.put("ABC","123");
finalHm.put("XYZ","789");
Now we can change the value of KEY "ABC".
finalHm.put("ABC","456");

But we can not assign a new reference.

finalHm = new HashMap();

If i am wrong please correct me.
 
Dan Craciun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chen

my answer was concerning prmitive fields. The best example is when you declare in a class
final double PI=3,14...; Then you can use it in another class to make mathematical calculations, but you cannot change its value (which makes sense, why would anyone change the value of PI ?).
You Q, was now if you are delaing with objects. In this case if you have for example
final String X=new String(); then you CANNOT do the following:
String Y=new String();
X=Y; // error.
So in the case of objects , final keyword deals with references. However you can change the content of String X !!!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Craciun wrote:Hi Chen

my answer was concerning prmitive fields. The best example is when you declare in a class
final double PI=3,14...; Then you can use it in another class to make mathematical calculations, but you cannot change its value (which makes sense, why would anyone change the value of PI ?).
You Q, was now if you are delaing with objects. In this case if you have for example
final String X=new String(); then you CANNOT do the following:
String Y=new String();
X=Y; // error.
So in the case of objects , final keyword deals with references. However you can change the content of String X !!!

Actually that's a bad example because Strings are immutable so you can't change the content of the String referred to by X. Replace String by StringBuilder and then everything Dan says is correct.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stevesean chen wrote:Thanks for the detail explanation, Dan Craciun. Really appreciate your help. So according to my understanding, private makes fields completely hidden away from outside of current class. Public allowed the access, and adding a final just prevent the fields from being modified outside of the current class.


Final means the field cannot be modified at all--inside or outside of the declaring class.

The access control (public/protected/((no keyword, meaning package-private))/private) determines who can even *see* that member--who can know that field, method, etc. exists and refer to it.

Completely independent of that, is final, which means "cannot be modified" for member variables and "cannot be overridden" for methods.

You almost always want to make all your fields private.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and final means “cannot be sub-classed” for classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!