JAVA

CONSTRUCTORS:

  • A special method that is called when an object is instantiated(created).

  • Works similar to a standard method call, except a constructor is a special method that works behind the scenes that will create an instance of a class

  • Will instantiate an object and can pass arguments when we instantiate it

Primary Uses of this:

  • Disambiguating Instance Variables from Local Parameters: When a constructor or method parameter has the same name as an instance variable, this is used to explicitly refer to the instance variable.

Java

    public class MyClass {
        String name;

        public MyClass(String name) {
            this.name = name; // 'this.name' refers to the instance variable
        }
    }
  • Invoking Current Class Constructor (Constructor Chaining): From within a constructor, this() can be used to call another constructor of the same class. This facilitates code reuse and promotes cleaner constructor implementations.

Java

    public class MyClass {
        int x;
        int y;

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

        public MyClass(int x) {
            this(x, 0); // Calls the two-argument constructor
        }
    }
  • Passing this as an Argument:

    this can be passed as an argument in a method call or a constructor call when the current object itself needs to be referenced by another object or method.

  • Returning the Current Class Instance:

    Methods can return this to allow for method chaining, where multiple method calls can be made on the same object in a single statement.

Important Note: The this keyword cannot be used in a static context (e.g., within static methods or static blocks) because static members belong to the class itself, not to a specific instance, and this refers to an instance.


In Java, public and private are two of the four access modifiers that control the visibility and accessibility of classes, fields, constructors, and methods.

1. public Access Modifier:

  • The public modifier grants the widest level of accessibility.

  • A public member (class, field, constructor, or method) can be accessed from any other class in the application, regardless of the package it belongs to.

  • This is typically used for elements that are intended to be part of a public API or interface, allowing other parts of the program, or even other programs, to interact with them.

Example:

Java

public class MyPublicClass {
    public int publicVariable;

    public void publicMethod() {
        // This method is accessible from anywhere
    }
}

2. private Access Modifier:

  • The private modifier provides the most restrictive level of access.

  • A private member (field, constructor, or method) can only be accessed from within the class in which it is declared.

  • This is primarily used to enforce encapsulation and data hiding, ensuring that internal implementation details of a class are not directly exposed or modified from outside the class.

  • private is often used for fields and helper methods that are exclusively used by the class itself.

Example:

Java

public class MyPrivateClass {
    private int privateVariable; // Only accessible within MyPrivateClass

    private void privateMethod() {
        // This method is only accessible within MyPrivateClass
    }

    public void publicMethodUsingPrivate() {
        privateMethod(); // Valid: calling a private method from within the same class
        System.out.println(privateVariable); // Valid: accessing a private variable from within the same class
    }
}

In summary:

  • public: Accessible from everywhere.

  • private: Accessible only within the declared class.