Instances and Access Modifiers
Creating a new object creates a new instance of that class. Because two different objects are separate instances, things can happen inside each one without affecting the other. We've already seen many cases where instances matter. For example, inside the constructor, like this:
JAVApublic class Pet {
String name;
int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
The this
keyword refers to the current instance of the class. Sometimes, you will want data or methods to remain inaccessible to other classes. This is where access modifiers come in to play.
Access Modifiers
Access modifiers change how a class, method, or property can be accessed by other classes. For example, we can restrict direct access to name
and weight
by marking them as private:
JAVApublic class Pet {
private String name;
private int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
Of course, these are properties you want accessible in some fashion. This is why getters (and setter) methods exist:
JAVApublic class Pet {
private String name;
private int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public int getWeight() {
return weight;
}
}
Which lets you get things like this:
JAVApet.getName();
pet.getWeight();
In this particular instance, it doesn't really add much value, but sometimes you want to do some validation or calculations using the property. In that case, you definitely want to restrict direct access of a property and instead make users go through specific methods.
Public
The public
access modifier is usable on classes, properties, methods, and constructors. This makes them accessible for all classes:
JAVApublic class Pet {
public String name;
public int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
Private
The private
access modifier is usable on properties, methods, and constructors. They make the code only accessible within that class:
JAVApublic class Pet {
private String name;
private int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
To access these values, the class needs to provide a public method inside that returns that value.
Protected
The protected
access modifier is usable on properties, methods, and constructors. They make the code only accessible within that class and any class that extends it:
JAVApublic class Pet {
protected String name;
protected int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
public class Cat extends Pet {
Cat(String name, int weight) {
super(name, weight);
}
}
Static
The static
makes properties and methods available for use without the need for instances or objects:
JAVApublic class Main {
public static void main(String[] args) {
hello();
}
public static void hello() {
System.out.println("Hello");
}
}
Final
The final
keyword makes it so that a defined property cannot be overridden:
JAVApublic class Main {
final static int example = 0;
public static void main(String[] args) {
example = 1; // this would fail
}
}
- Getting Started with Solid
- Getting Started with Electron
- Git Tutorial: Learn how to use Version Control
- Best Visual Studio Code Extensions for 2022
- How to deploy a Deno app using Docker
- How to deploy a MySQL Server using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- Using Puppeteer and Jest for End-to-End Testing
- Getting Started with Handlebars.js
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Learn how to build a Slack Bot using Node.js