2

I am relatively new to java and I did some reading about private and public acccess modifiers. I would like to eliminate any confusion once and forall in this topic because I feel like I don't have the best grasp on it.

Access modifiers in variables

Please correct me if I am wrong, a variable is public on default. If the access modifier is set to public or just not set at all, than other classes from the same project can access the integer and/or modify it. If it is private than it is not visible to the outside and cannot be accessed by classes outside the one in which it was created.

Accesss modifiers in classes

Access modifiers in classes, I don't seem to fully understand. As far as my understanding, if I call a private method from method that is not in the same class, than it will not work. If it is public than it will?

Is there anything I am missing or don't understand correctly?

I appreciate help in this regard.

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • Variables only exist inside functions. You mean _fields_ – SLaks May 24 '12 at 19:46
  • 2
    @SLaks: No, fields are variables too. See http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.3 – Jon Skeet May 24 '12 at 19:51
  • Last question, classes, methods, and variables can all have access modifiers, correct? – AnchovyLegend May 24 '12 at 20:00
  • @AnchovyLegend, yes (but not *local* variables, only fields). [Here](http://stackoverflow.com/a/33627846/276052) is a reference table that is slightly more clear than the one in the official tutorial. – aioobe Nov 13 '15 at 12:32

8 Answers8

3

Please correct me if I am wrong, a variable is public on default.

You're wrong. Assuming you mean fields, by default, they have "package" access, which can't be expressed explicitly. Local variables have no concept of access control - they only exist within the context of a method anyway, so can't be referred to from anywhere else.

If the access modifier is set to public or just not set at all, than other classes from the same project can access the integer and/or modify it.

If it's set public, then any code can access it.

If it's default (package) access, then any code in the same package can access it.

Access modifiers in classes, I don't seem to fully understand. As far as my understanding, if I call a private method from method that is not in the same class, than it will not work. If it is public than it will?

That's pretty much right, yes.

I suggest you read the Java tutorial on all of this, and consult the language spec section 6.6 for more details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

Here's a good doc on the subject.

There are four access levels:

  1. private: Only that class (not even descendants) can access.
  2. protected: Only that class and its descendants and classes in the same package can access.
  3. package-private (no specifier)--only classes in the same package can access--even subclasses that are not in the same package cannot.
  4. public: Everything can access.

For for both classes member variables and methods, the default access is package private.

SirPentor
  • 1,994
  • 14
  • 24
1

You can find information about the 4 modifiers here on Oracles website.

If you do not specify the modifier, it is said to be on default, which means only any code in the same package can access it.

If it's set public, then any code in any package can access it.

Basilio German
  • 1,801
  • 1
  • 13
  • 22
1

Actually in Java there are four different access modifiers, private, public, protected and package specific. "Please correct me if I am wrong, a variable is public on default" - you are wrong here a variable if not declared differently has a package related access. If it is set to public then it can be modified by other classes even outside of the original package. This website - http://javapapers.com/core-java/access-modifiers-in-java-explain/ - can provide you useful hints. Actually it is very easy once you read a bit more on OO concepts such as Encapsulation. Then you will understand the purpose of access modifiers (to ensure data integerity).

aretai
  • 1,619
  • 6
  • 19
  • 30
0

Access Modifiers:

  • Public - {Can access anywhere in the project}

    Private - {Can access only inside the class}

    Protected - {Can access within the package and sub classes}

    Default - {can access within the package}

Non-Access Modifiers:

  • Static - {for creating class variable and method}

    Final - {for creating finalized class, variable and method}

    Abstract - {for creating abstract method and class}

    Synchronized - {for threads}

To learn more follow this link

Community
  • 1
  • 1
Hariprasath
  • 828
  • 4
  • 15
  • 41
0

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

Default : Visible to the package. No modifiers are needed.

Private : Visible to the class only.

Public : Visible to the world. All classes and packages.

Protected : Visible to the package and all subclasses.

If no access modifier is specified by the programmer, default access modifier is used.

Atiq
  • 166
  • 2
  • 6
  • 21
0

private and public java access specifiers.in java,private access specifiers it specifies the access.private is like own family property that can uses only which family belongs.that means inside the class,method and variable can be used. public is like government properties that means every one can access the properties without any need of tokens

0

private and public java access specifiers.in java,private access specifiers it specifies the access.

private is like own family property that can uses only which family belongs.that means inside the class,method and variable can be used

.

public is like government properties that means every one can access the properties without any need of tokens