0

Our lecturer showed us this code yesterday. And I didn't understand why he wrote extra access modifiers. When I delete some parts of code, it is still running.

public class Counter {
private final String name;
private int count;

public Counter(String id) {
    name = id;
    count = 0;
}

public void increment() {
    count += 1;
}

public int tally() {
    return count;
}

public String toString() {
    return count + " " + name;
}

public static void main(String[] args) {
    Counter c0 = new Counter("first");
    Counter c1 = new Counter("second");

    c0.increment();
    c0.increment();
    c1.increment();

    System.out.println("c0 counter is " + c0);
    System.out.println("c1 counter is " + c1);
}

then I delete access modifiers and;

   public static void main(String[] args) {
    Counter c0 = new Counter("first");
    Counter c1 = new Counter("second");

    c0.increment();
    c0.increment();
    c1.increment();

    System.out.println("c0 counter is " + c0);
    System.out.println("c1 counter is " + c1);
}

the code is still running. Actually I'm not very good at this so can someone tell basically?

Ulas Bayram
  • 1
  • 1
  • 1
  • 2
    What part of the code you deleted , the private and final keywords ? – nits.kk Mar 27 '16 at 20:09
  • 1
    What access modifiers did you delete. Not enough information – pczeus Mar 27 '16 at 20:09
  • 3
    Everything is in one class; `private` is the most *restrictive* access modifier, but that doesn't matter in a program with only one class. – Elliott Frisch Mar 27 '16 at 20:09
  • Possible duplicate of [Which is the default access specifier in Java?](http://stackoverflow.com/questions/3530065/which-is-the-default-access-specifier-in-java) – Mick Mnemonic Mar 27 '16 at 20:14
  • @MickMnemonic It is not a duplicate. It is hardly even related. – RaminS Mar 27 '16 at 20:53
  • @Gendarme, of course it's related. One of the most confusing things about Java's access modifiers is _package private_ a.k.a "default". Based on the title, that's exactly what the OP is wondering. – Mick Mnemonic Mar 27 '16 at 20:56

4 Answers4

2

Access modifiers control what things outside the class can access them. The class itself can access everything.

RaminS
  • 2,208
  • 4
  • 22
  • 30
1

By removing access modifiers on the class or variables, you are allowing Java to use the default: package access only for classes, public for interfaces.

Since your main is in the same class, it still has access so everything still runs successfully.

For more information:

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

Visible to the package. the default. No modifiers are needed.

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

Community
  • 1
  • 1
pczeus
  • 7,709
  • 4
  • 36
  • 51
0

If you don't use an access modifier, the method or attribute can be accessed by every class in the same package. I guess your main() is in the same package, that's why it works.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • Not only is it in the same package, but it is in the same class. Access modifiers have no effect what so ever. – RaminS Mar 27 '16 at 20:18
  • @Gendarme: Yes, probably it is, however th OP seems to have forgotten the closing brace of the class, so you cannot be sure. – Frank Puffer Mar 27 '16 at 20:23
0

There are four access modifiers in Java: public, private, protected and package private. When not specified explicitly the visibility is package private by default.

PotatoProgrammer
  • 124
  • 1
  • 1
  • 7