Chapter[7]: Essential Java Keywords Explained: this, static, main, public, class, and the Dot Operator

Automate This. By Mrigank Saxena
4 min readNov 30, 2024

Java is a powerful programming language with specific keywords and concepts that form the backbone of writing effective code. In this article, we break down the significance and usage of critical Java keywords: this, static, main, public, class, and the dot operator (.). Understanding these will help you write better programs, avoid errors, and grasp how Java works under the hood, I revisit these keywords often because grasping certain OOP concepts can be challenging for non-OOP coders — I’ve been in your shoes. Exploring different perspectives helps things make sense.

1. this Keyword in Java

What is it?

The this keyword is used to refer to the current object in a method or constructor. It is vital for distinguishing between instance variables and parameters with the same name.

Usage Example:

public class Car {
private String make;
private int makeYear;
public Car(String make) {
this.make = make; // Assigning parameter to the instance variable(when parameter and instance variable names are same)
}

public Car(int year) {
makeYear = year; // Assigning parameter to the instance variable(when parameter and instance variable names are not same)
}
}

How It Helps:

  • Avoids Confusion: When local variables shadow instance variables, this ensures you're referring to the instance variable.
  • Supports Constructor Chaining: You can call one constructor from another using this , and it’s way useful then you can imagine.

What Happens if Not Used?

Without this, instance variables may remain uninitialized when their names are shadowed by parameters. For example:

public Car(String make) {
make = make; // Both refer to the parameter, leaving the instance variable unassigned.
}

2. static Keyword

What is it?

The static keyword indicates that a field or method belongs to the class rather than an instance of the class. Static members are shared across all instances of the class.

Usage Example:

public class Car {
public static int totalCars = 0;

public Car() {
totalCars++;
}
}

How It Helps:

  • Global Access: A static field or method can be accessed without creating an instance.
  • Memory Efficiency: Static variables are created once per class, not per object.
  • Entry Point: The main method is static because the JVM calls it without creating an object.

What Happens if Not Used?

If main is not static, the program won't run:

public void main(String[] args) {
// JVM cannot invoke non-static methods without an object.
}

3. main Method

What is it?

The main method is the entry point for any Java application. It is where program execution starts.

Usage Example:

public static void main(String[] args) {
Car myCar = new Car("Toyota");
System.out.println("Car make: " + myCar.make);
}

How It Helps:

  • Defines Execution Start: Without main, the JVM won’t know where to begin.
  • Supports Arguments: The args parameter allows you to pass data to your program from the command line.

What Happens if Not Used?

Without a valid main method, the JVM will throw an error like:

Error: Main method not found in class

4. public Keyword

What is it?

The public access modifier allows a class, method, or field to be accessible from anywhere in the program.

Usage Example:

public class Car {
public String make;

public Car(String make) {
this.make = make;
}
}

How It Helps:

  • Ensures Accessibility: A public class can be used in any other class or package.
  • Main Method Visibility: The main method must be public for the JVM to access it.

What Happens if Not Used?

  • If the main method isn’t public, the JVM cannot run it:
static void main(String[] args) {
// JVM won't execute this method.
}
  • If a class isn’t public, it won’t be accessible outside its package.

5. class Keyword

What is it?

The class keyword defines a blueprint for objects. It groups data (fields) and behaviors (methods).

Usage Example:

public class Car {
private String make;

public Car(String make) {
this.make = make;
}
}

How It Helps:

  • Core Structure: Classes are the foundation of Java’s object-oriented programming (OOP).
  • Encapsulation: Classes bundle data and methods, keeping them secure and modular.

What Happens if Not Used?

Without class, Java programs cannot define objects or behaviors. The compiler will throw errors for missing syntax.

6. Dot Operator (.)

What is it?

The dot operator (.) is used to access an object’s fields and methods.

Usage Example:

Car myCar = new Car("Toyota");
System.out.println(myCar.make); //will print Toyota

How It Helps:

  • Field Access: Accesses object attributes (e.g., myCar.make).
  • Method Invocation: Calls an object’s methods (e.g., myCar.start()).

What Happens if Not Used?

Without the dot operator, you cannot interact with object members. For example:

System.out.println(make); // Compiler doesn't know which object's make to use.

Alternative Way to Assign Parameters to Instance Variables

For beginners, instead of using a constructor, you can manually assign values to fields like this:

public class Car {
public String make;

public void setMake(String make) {
this.make = make; // Explicit assignment method, ti assign value we must call the method, and pass the desired value, suppose we have hundredes of attributes, that may force to create 100 methods, and 100 calls.
}
}

public Car(String make) {
this.make=make; // assignment automatically done in constructor when we create an object
}

Drawbacks:

  • Verbose Code: Each field needs a separate method.
  • Error-Prone: Forgetting to call the setter can leave the field uninitialized.
  • Manual Work: Constructors streamline initialization, reducing repetitive code.

By mastering these Java essentials, you’ll write clean, efficient, and maintainable code while avoiding common pitfalls. These keywords form the foundation of Java programming, making them indispensable for beginners and experts alike!

You can now catch the podcast on YouTube too!

.

.

.

Happy Coding!

--

--

Automate This. By Mrigank Saxena
Automate This. By Mrigank Saxena

Written by Automate This. By Mrigank Saxena

Join me as I share insights, tips, and experiences from my journey in quality assurance, automation, and coding! https://www.linkedin.com/in/iammriganksaxena/

No responses yet