2 min read

Java Basics 1

Java Basics 1
Java Basics 1

What are the static block, static method, and static variable? Tell difference.

A static block, static method, and static variable are all related to the concept of static members in Java.

A static block is a block of code that is executed when a class is first loaded by the JVM. It is used to initialize static variables and perform other one-time setup tasks for the class. A static block is identified by the keyword 'static' and it is executed only once in the lifetime of the class.

A static method is a method that is associated with a class rather than an instance of the class. It can be called directly on the class without the need to instantiate an object. A static method is identified by the keyword 'static' in its declaration and can access only static variables of the class.

A static variable is a variable that is associated with a class rather than an instance of the class. It is created when the class is loaded by the JVM and is shared among all instances of the class. A static variable is identified by the keyword 'static' in its declaration.

The main difference between these three concepts is their scope and accessibility. A static block is used to initialize the class-level state and is executed only once, a static method can be called directly from the class and can only access the class-level state, and a static variable is a class-level variable that is shared among all instances of the class.

What is Constructor and what are the different types?

A constructor is a special type of method that is used to create and initialize an object of a class. It is automatically called when an object is created using the "new" keyword, and it can be used to set the initial state of the object.

In Java, there are several types of constructors:

  1. Default Constructor: A constructor with no parameters is known as a default constructor. If a class doesn't have any constructor then the compiler provides a default constructor with no arguments.
Test t = new Test();
  1. Parameterized Constructor: A constructor with parameters is known as a parameterized constructor. It is used to initialize the object with specific values when it is created.
Car(String carColor)
    {
        this.carColor = carColor;
    }
  1. Copy Constructor: A constructor that creates an object by copying values from another object is known as a copy constructor.
  2. Private Constructor: A constructor that is declared as private is known as a private constructor. It can be used to prevent the instantiation of a class from outside the class, for example, for creating Singleton classes.
  3. Static Constructor: Not a real constructor, but a class can have a static block that is executed only once when the class is loaded.

It is important to note that in Java, constructors do not have a return type, not even void, and they have the same name as the class they are defined in. Additionally, constructors can be overloaded (i.e. multiple constructors can be defined with different parameters), allowing for more flexibility in creating objects.