Dot Notation

🙋 Need help? Ask an expert now!

"Dot notation" is an important concept in Java and something that must be paid attention to when creating objects. The members of the class, including variables and methods, are accessed using dot notation. Let's look at a few examples of dot notation.

package exlcode;

public class DotNotationExample {

  public static void main(String[] args) {
    DotNotationTest dotNotationTest = new DotNotationTest();
    // any public variable or method in DotNotationTest can be
    // accessed through dot notation
package exlcode;

public class DotNotationTest {

  public String exampleVariableOne = "Objects";
  public int exampleVariableTwo = 20;
  private int exampleVariableThree = 10;

  public void print() {
    System.out.println("Hello World!");

If you recollect from the section on Access Control, only the public methods and variables can be accessed through the object. Private methods are not accessible. For classes that are included in Java packages, you can find the documentation online to see what the available public method and variables are. And you can make sure the variables in your classes are either set as public or private depending on whether or not you want to allow the object to access it and change its data if needed. When you attempt to access a private member of a class, you will throw a runtime error.

Also, when you see Java reserved word this like this.variablename, it is a reference to a current object whose method or constructor is being invoked. It's usually unnecessary to use this reserved word but make sure you know how it works.

Application Question

Consider the following class declarations:

public class Main {
              public static void main (String[] args){
                ExampleClass exampleClassOne = new ExampleClass();
            public class ExampleClass {
              public int varOne = 10;
              private String varTwo = "Hello World";
              private boolean varThree = false;
              public String getString(){
                return varTwo;
              public boolean getBool(){
                return varThree;

Which statement in the Main class will cause an error?