The basics of Java access modifiers

Photo by Jason Pofahl on Unsplash
package com.example;import java.io.Serializable;public class ToyExample implements Serializable {

private static final long serialVersionUID
= 4550389864381561651L;

public static final int INTEGER_CONSTANT = 1729;

protected int someInteger = -58;

double someNumber = Math.random() * someInteger;

private static int instantiationCount = 0;

public static int getInstantiationCount() {
return instantiationCount;
}

public ToyExample() {
instantiationCount++;
this.someNumber *= instantiationCount;
}

}
package various;import com.example.ToyExample;public class AccessDemo {

public static void main(String[] args) {
System.out.println(ToyExample.INTEGER_CONSTANT);
System.out.println(ToyExample.getInstantiationCount());
ToyExample easyExample = new ToyExample();
System.out.println(ToyExample.getInstantiationCount());
System.out.println(easyExample.someInteger); // ERROR!
System.out.println(easyExample.someNumber); // ERROR!
System.out.println(easyExample.instantiationCount);
// ERROR!
}

}
package com.example;import java.io.Serializable;public class ToyExample implements Serializable {

private static final long serialVersionUID
= 4550389864381561651L;
// ... other fields omitted from quotation ... @Override
public String toString() {
String str = this.instanceLabel;
if (this.instanceFlag) {
str = str + this.instanceWeight;
}
return str;
}

public static final int getInstantiationCount() {
return instantiationCount;
}

protected void adjustInteger(int integer) {
this.someInteger = integer;
}

void adjustNumber(double number) {
this.someNumber = number;
}

private void makeNumbersEquals() {
this.someNumber = this.someInteger;
}
// ... constructor omitted from quotation ...}
package com.example;public class SamePkgSub extends ToyExample {

@Override
protected void adjustInteger(int integer) {
this.someInteger &= 65535;
}

@Override
void adjustNumber(double number) {
this.someNumber = Math.exp(number);
}

}
package various;import com.example.SamePkgSub;
import com.example.ToyExample;
public class AccessDemo {

public static void main(String[] args) {
System.out.println(ToyExample.getInstantiationCount());
ToyExample easyExample = new ToyExample();
System.out.println(ToyExample.getInstantiationCount());
ToyExample samePkgSub = new SamePkgSub();
System.out.println(ToyExample.getInstantiationCount());
ToyExample diffPkgSub = new DiffPkgSub();
System.out.println(ToyExample.getInstantiationCount());
samePkgSub.adjustInteger(12); // ERROR!
diffPkgSub.adjustInteger(13); // ALSO AN ERROR

}

}
package time;public enum DayOfTheWeek {

SUNDAY ("Su", '\u2609'),
MONDAY ("M", '\u263E'),
TUESDAY ("Tu", '\u2642'),
WEDNESDAY ("W", '\u263F'),
THUSDAY ("Th", '\u2643'),
FRIDAY ("F", '\u2640'),
SATURDAY ("Sa", '\u2644');

private final String abbreviation;

private final char alchemSymbol;

public String getAbbreviation() {
return this.abbreviation;
}

public char getSymbol() {
return this.alchemSymbol;
}
DayOfTheWeek(String abbrev, char symbol) {
this.abbreviation = abbrev;
this.alchemSymbol = symbol;
}

}
Screenshot of the Navigator pane in NetBeans 11.2.
Screenshot of the Navigator pane in NetBeans 11.2.
Left: Detail of the Navigator pane for a regular class with four constructors in NetBeans. Right: Also in NetBeans, detail of the Navigator pane for an enumerated type, showing the constructor is regarded by NetBeans as private, even though it’s not explicitly marked as such.
    private final String instanceLabel;

private final double instanceWeight;

private final boolean instanceFlag;
    public ToyExample() {
this("", 0.0, false);
}

protected ToyExample(String label) {
this(label, 1.0, true);
}

ToyExample(String label, double weight) {
this(label, weight, true);
}

private ToyExample(String label, double weight, boolean flag) {
this.instanceLabel = label;
this.instanceWeight = weight;
this.instanceFlag = flag;
instantiationCount++;
this.someNumber *= instantiationCount;
}
package various;import com.example.SamePkgSub;
import com.example.ToyExample;
public class AccessDemo {

public static void main(String[] args) {
System.out.println(ToyExample.getInstantiationCount());
ToyExample easyExample = new ToyExample();
System.out.println(ToyExample.getInstantiationCount());
ToyExample samePkgSub = new SamePkgSub();
System.out.println(ToyExample.getInstantiationCount());
ToyExample diffPkgSub = new DiffPkgSub();
System.out.println(ToyExample.getInstantiationCount());
ToyExample anonymousExample
= new ToyExample("Anonymous Example") {

// WARNING: No equals() override

@Override
public int hashCode() {
int addend = ToyExample.INTEGER_CONSTANT
* this.someInteger;
}

};

System.out.println(easyExample.someInteger); // ERROR!!!
System.out.println(anonymousExample.someInteger);
// ALSO AN ERROR
}

}
public class ToyExample {

private int counter;

public void advanceCounter(int increment) {
this.counter += increment;
}

public ToyExample(int initCounter) {
this.counter = initCounter;
}

class NestedNonStatic {

public void revealCounter() {
System.out.println(counter);
}

}

static class NestedStatic {

public void displayCounter() {
System.out.println(counter); // ERROR!!!
}

}


}
package com.example;public interface ItemSupplier {

public abstract Item supply(Parameter... parameters);
}

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store