Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Dot Notation
Introduction To Java Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Introduction To Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
"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.
DotNotationExample.java
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
System.out.println(dotNotationTest.exampleVariableOne);
System.out.println(dotNotationTest.exampleVariableTwo);
dotNotationTest.print();
}
}
DotNotationTest.java
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.