public modifier—the field is accessible from all classes.
private modifier—the field is accessible only within its own class.
the question wasn't "What's the difference between..." the question was
Nosson1459 wrote...
What's wrong with public variables.
. I read that whole page of documentation (and mostly the links from it) and if that's the only difference between them then: Why are YOU (and others) so strict about not using public variables? How does it effect me to have all my variables public? If I want to use the variable in another class I could BUT if I don't want to then I just won't call the variable in my source code for the other class (why do I have to make it that I'll get a syntax error if I try (<-- this is not the only or main question of this post)).
There is nothing absolutely "wrong" with making fields public, in your case. It is just not recommended -- programmers should get used to encapsulating data as it will be much more important when building support classes and libraries.
If you are building class libraries, then it is recommended practice to encapsulate a variable with getters and setters and set it as private, this will insure that you are in full control of your class at all times and prevents errors from occurring due to external code changing internal library attributes. That is why you should use private with getter and setter methods over public variables
Do I have to write public or private, or can I just leave it blank? Basically, Why can't I just not type anything instead of typing private (I know there is a difference but still)?
Do I have to write public or private, or can I just leave it blank? Basically, Why can't I just not type anything instead of typing private (I know there is a difference but still)?
Again, there is absolutely nothing "wrong" with whatever access privileges you give the members of your classes (except for overriding methods, which cannot be given weaker access).
So why does everyone always write "private" in Greenfoot instead of leaving it blank (If you're on the site now could you look at my other discussions).
So why does everyone always write "private" in Greenfoot instead of leaving it blank (If you're on the site now could you look at my other discussions).
Like I said before, you should get used to giving minimum access to all the members of your classes. It is good programming practice and will help later on as you progress as a programmer.
What level of publicity is not typing anything, is it more than "public" less than "private" or in between?
When you do not include an access modifier, the access is called 'package-private'. This is less access than 'public', which can be accessed from anywhere within or outside the members package; and it is more than 'private', which can only be accessed from within its class. There is also a 'protected' access modifier, which can be accessed from its own class and any subclass of that class.