0

I just though about it: if you have class like this:

public class a
{
    int x = 5;
    //setter & getter
}

you can't access to x from another class unless it is public.
so, that i know, but if it is like that, why do i need to use private?

the x variable cannot be accessed outside of the class without public or in other cases static reserved words.

so, the question(s):
what do i need the private for? do i have to use the private or it doesn't really matter?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • The `private` is **not** mandatory in **C#**, but when reading through your code in the future and troubleshooting it, would you rather have to remember that `private` is implied? Or *explicitly* see `private` on the field and know what is happening? – Der Kommissar Apr 24 '15 at 14:27
  • The default in Java (no keyword) is package-private, not private. This is explained in the linked duplicate. – Roger Lipscombe Apr 24 '15 at 19:55
  • @RogerLipscombe sorry, i searched in google and i didn't see it. anyway, i got my answer, ty :) –  Apr 25 '15 at 08:37
  • A comparison between `public`, `private` and the default access (which is in effect in this case) is given [here](http://stackoverflow.com/a/33627846/276052). Hope that helps! – aioobe Jun 10 '16 at 17:14

4 Answers4

3

No access specifier is not the same as private.

When you have no access specifier, as in your example, the variable x is accessible in class a, but also in all other classes that are in the same package as class a.

When it is private, it is only accessible in class a, and not in any other class.

See Controlling Access to Members of a Class in Oracle's Java Tutorials.

Jesper
  • 202,709
  • 46
  • 318
  • 350
0

By leaving out the access modifier, you are relying on the language's default.

For example, in C# class variables default to private.

Spivonious
  • 718
  • 7
  • 20
  • But in Java, the default access level is not the same as `private`. – Jesper Apr 24 '15 at 14:26
  • That's, as a generic, global statement is false. class members in C# default to the protection of the parent class. If a class is not defined as public the member will default to a private state, this is not quite the same as private but they are not public. Interfaces on the other hand default to public and cannot be private. – CalebB Apr 24 '15 at 14:28
  • Correct CalebB. I'll edit my answer. – Spivonious Apr 24 '15 at 14:31
0

The most used access modifiers are undoubtedly public and private.

public means you expose your field or method to every other class. This is useful for methods that are necessary to let the class perform its major function(s) and, in some cases, for constants that need to be visible.

private means you hide the member from every other class. This is useful for internal state and functions which other classes must not know about. This enforces the concept of information hiding.

The other modifiers protected and default (when no modifier is mentioned) are intermediate levels of access which can be useful in specific cases.

For example when sub classes need access to a method in the parent (or base) class and you don't want other classes to be able to access it, you can make it protected.

The default access modifier is more restricting than protected and can be used to allow access only from within the same package. This can be useful if a package contains a group of tightly related classes which need to interact among each other via methods which you don't want to expose elsewhere, not even to sub classes outside this package.

Maybe you will also benefit from this article.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
-1

this question is already answered in stackoverflow in this link.

There is 4 types of access restrictions in java:

  • public: the attribute is accessible for all clients
  • private: the attribute is only accessible from the class, not subclasses.
  • protected: the attribute is accessible from package and subclasses.
  • no modifier: the attribute is accessible from the class and package.

It's necessary to declare the scope of accessibility to encapsulate the functionality correctly.

Community
  • 1
  • 1
jort
  • 1
  • 2
  • This is incorrect. There are actually four levels: `public`, `protected`, `private` and no access specifier. The default (no access specifier) is not the same as `protected`. See the answer in the link you provided. – Jesper Apr 24 '15 at 14:44
  • Sorry, thanks for the tip. – jort Apr 24 '15 at 15:06