Private method access instance variable in Java 20

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Java 20

In Java 20, instance variables can be accessed in a private method by using the 'this' keyword. The 'this' keyword refers to the current object and can be used to access instance variables within private or public methods of the class.

Here's an example code that demonstrates how to access instance variables in a private method in Java 20:

class MyClass {
    // instance variables
    private int x;
    private int y;

// constructor
public MyClass(int x, int y) {
this.x = x;
this.y = y;
}

// private method that accesses instance variables
private void updateXandY(int newX, int newY) {
this.x = newX;
this.y = newY;
}

// public method that calls private method and prints instance variables
public void updateCoords(int newX, int newY) {
this.updateXandY(newX, newY);
System.out.println("New coordinates: (" + this.x + ", " + this.y + ")");
}

// main method
public static void main(String[] args) {
MyClass object = new MyClass(10, 20);
object.updateCoords(30, 40); // calls public method
}
}

In the example above, the private method updateXandY() is called by the public method updateCoords(), which updates the values of the instance variables x and y. The this keyword is used to access the instance variables within the private method. Finally, the updated values of the instance variables are printed using System.out.println().