Java Java Objects Meet Objects Final

Adam Thompson
Adam Thompson
415 Points

Why would I want to block others from changing variables, etc?

In the recent videos, techniques were provided to block coders from doing certain things, such as accessing or modifying a variable. Why would I want or need to block a coder from modifying a variable? If I've given him access to my code, it seems that I want/need him to change it, and it would be easier to just let him decide what he needs to do, rather than me decide in advance what he can and cannot do.

3 Answers

Nicholas Grenwalt
Nicholas Grenwalt
46,625 Points

Yeah, this isn't for when you are having a buddy check out your code per se, but rather for when your code is out in the wild it is great practice. If you don't privatize your variables then you give them free range to take advantage of anything they like. For instance, let's say that you store a discount in a variable that had 10% and they went in and switched it to 100% prior to submitting it. Giving them access to your variables allows them to manipulate your code in ways that it wasn't designed for and possibly exploit vulnerabilities to their advantage or even allow them to send malicious content back to the server.

Adam Thompson
Adam Thompson
415 Points

If I write a program in Java, the .java source files wouldn't be distributed out into the wild, would they? So how would someone access my variables?

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
20,998 Points

You need to seal your main variable and its structure there may be an important data that you may not want to change with first shot