Need a little help understanding what "private" means in Java
20 replies, posted
So I've just started out with Java, and it's great and all that. I'm up to the point where they mention something known as "encapsulation" (I'm learning from a book), and they use the word "private" alot. What does this mean and what does it do?
It means functions, etc. outside the class can't access it.
[QUOTE=thf;24429530]It means functions, etc. outside the class can't access it.[/QUOTE]
If it's not too much trouble, could I have an example?
I've always wondered...
WHY use private?
^Encapsulation...
Sorry, I worded it incorrectly, I was saying that that was something he should look into as well.
[QUOTE=geel9;24429622]I've always wondered...
WHY use private?[/QUOTE]
For internal variables and methods so they can't be acessed outside of the class. This is the base idea of encapsulation: You only have access to what you need to have access to.
ok, here is a quick example:
run.java:[code]public class java {
public static void main(String[] args) {
commands com = new commands();
System.out.println("Total: " + com.getTotal());//Public command
System.out.println("Num1: " + com.num1);//Public variable
System.out.println("Num2: " + com.num2);//Private variable. It will error here
}
}[/code]
commands.java:[code]
public class commands {
public num1 = 1;
private num2 = 2;
public commands(){}
public static int getTotal() { return number + num2; }
}[/code]
[editline]10:39AM[/editline]
sorry for its roughness. didnt have any offhand examples... hope it explains it to you
[QUOTE=XCrashFur;24429547]If it's not too much trouble, could I have an example?[/QUOTE]
[cpp]class foo
{
private void doSomethingPrivate(){ ... }
protected void doSomethingProtected(){ doSomethingPrivate(); } //ok
public void doSomethingPublic(){ doSomethingPrivate(); } //ok
}
class bar : foo
{
void baz()
{
doSomethingPrivate(); //error
doSomethingProtected(); //ok
doSomethingPublic(); //ok
}
class unrelated
{
void baz()
{
bar b = new bar();
b.doSomethingPrivate(); //error
b.doSomethingProtected(); //error
b.doSomethingPublic(); //ok
}
}[/cpp]
Private stays in that one class only, protected lets you also access it from class inheriting and public is basically visible everywhere.
[QUOTE=geel9;24429622]I've always wondered...
WHY use private?[/QUOTE]
Example:
I have a path-iterator, which can iterate over files or folders via a given DOS Regex pattern.
For this I have created a function checking if the current path matches the pattern and returns true if the pattern matches (false otherwise).
Now I can simply do
[cpp]basic_search_iterator& operator++()
{
do
++internal_iter;
while(!matches_pattern());
return *this;
}
basic_search_iterator& operator--()
{
do
--internal_iter;
while(!matches_pattern());
return *this;
}[/cpp]
Though there is no reason for something on the outside to see if the pattern matches, because it will always be true for them.
internal_iter is also private; it's a basic path-iterator which iterates over every path found in a directory without a pattern (or * if you want). There's no reason for someone on the outside to know what's going on internally.
It'd also be bad if someone on the outside messed with it, possibly changing the conditions (not matching the pattern anymore) ;)
Goddammit read my OTHER post in this thread.
Well excuse me that writing my post took longer than you guys posting.
Also, express yourself more accurately next time :colbert:
I apologize :saddowns:
Accepted :buddy:
[img]http://www.facepunch.com/image.php?u=65545&dateline=1151071332[/img]:respek:[img]http://www.facepunch.com/image.php?u=154187&dateline=1274606722[/img]
[editline]08:58AM[/editline]
My avatar is really bad. I need to change it.
...I posted an example already...
[QUOTE=CountNoobula;24462516]...I posted an example already...[/QUOTE]
I hope you're joking because
[QUOTE=ZeekyHBomb;24429864]Well excuse me that writing my post took longer than you guys posting.[/QUOTE]
also, my example includes more stuff that yours :smug:
[QUOTE=CountNoobula;24462516]...I posted an example already...[/QUOTE]
imo, your's doesn't explain it very well. I think Zeeky's example is a shitload better than yours.
[QUOTE=Agent766;24463467]imo, your's doesn't explain it very well. I think Zeeky's example is a shitload better than yours.[/QUOTE]
Indeed.
Zeeky seems to be more experienced, anyway, so he's obviously more correct. :smile:
I like to think that private variables were made because programmers were getting sick of others messing up their classes by modifying variables they shouldn't be.
Also, if you're working on a project with other people and you're making a certain class, the others aren't going to know what variables they shouldn't mess around when using an instance of your class, so making any variable that they don't need to use private makes it a lot easier for people to use the class because they don't have a ton of variables and methods that they aren't going to use, and it is going to make your class less error prone.
[QUOTE=esalaka;24463755]Indeed.
Zeeky seems to be more experienced, anyway, so he's obviously more correct. :smile:[/QUOTE]
Mao Zedong is very experienced at running a country.
[QUOTE=turb_;24484621]Mao Zedong is very experienced at running a country.[/QUOTE]
Mao is dead, your argument is invalid :smile:
Sorry, you need to Log In to post a reply to this thread.