Let's Learn and Grow??

Java ??

Daily_Learning_3;

Understanding Constructors in Java ???

A constructor is a special method utilized to initialize an object of a class and define the values of its instance variables.

In Java, constructors are invoked during the creation of a new object. They play a crucial role in providing data for the object by constructing the values for its instance variables, hence earning the name "constructor."

Breaking it Down ??

class Circle { ... }: This is the blueprint for creating circles in our Java program. ??

public Circle(float r) { ... }: This line defines the constructor. It's like a special function with the same name as the class. Here, float r is a parameter representing the radius of the circle. ??

radius = r;: Inside the constructor, radius, which is a property of the Circle class, is set to the value provided when creating a circle. ??

Now, whenever you create a Circle object using new Circle(), the constructor will automatically be called, setting up the radius for that circle. ???

This Keyword ??

  • The 'this' keyword is often used in classes to refer to instance variables. It's used to distinguish the instance variables from parameters that have the same names as the instance variables, making the declaration of a constructor easier. ???
  • The 'this' keyword is used to refer to the current class instance variable. It resolves ambiguity between instance variables and parameters. ??
  • For example, if there's an instance variable named salary declared inside a class, it can be referred to as this.salary in all methods within that class. ??
  • Another example: If you see code like 'this.variableName = variableName', it's setting the value of the instance variable 'variableName' to the value of the parameter 'variableName'. ??

Circle Class: ??

  • Represents a circle and contains a field named radius to store the radius of the circle. ??
  • Provides a constructor that takes the radius as a parameter and initializes the radius field. The usage of this.radius = radius; ensures that the parameter radius is assigned to the instance variable this.radius. ???
  • Includes a method named area that calculates and returns the area of the circle using the formula π * radius^2. Within this method, this.radius is used to access the radius of the circle object for which the area is being calculated. ??
  • Overall, the program creates three circles with radii 10, 20, and 15, respectively, calculates their areas, and prints the results. ??
  • The constructor we declared in the last segment is known as a parameterized constructor. These constructors take in values in the form of input parameters for initializing the instance variables. ??
  • A constructor is used to initialize the state of an object. Constructors are of two types: ???

Now let's see the below code

Parameterized constructor ??

A constructor that has parameters is known as a parameterized constructor. A parameterized constructor is used to assign different values to the instance variables of distinct objects.

This constructor accepts a parameter radius and sets the value of the radius field in the Circle class to the provided value.

Used when you want to create a circle with a specific radius.

public Circle(float radius) {

this.radius = radius;

}

Default constructor???

A constructor that has no parameter and does nothing apart from creating a new object is known as a default constructor. It is a method that need not be declared if there is no parameterized constructor, or you can declare it and leave it empty in case of a declaration of any other parameterized constructor. Default constructors assign default values to the instance variables of the objects depending on the type, for example, 0, null, etc.:

This constructor doesn't accept any parameters and doesn't do anything explicitly.

Used when you want to create a circle without specifying the radius immediately.

public Circle() {

// Default constructor

}

  • Created three instances of the Circle class: c1, c2, and c3. ??
  • c1 and c2 are created with specific radii (10f and 20f), using the parameterized constructor. ??
  • c3 is created using the default constructor, which means its radius field remains uninitialized. ??
  • Calculate and print the areas of all three circles using the area() method. ??
  • When you create c3 using the default constructor, its radius field is not initialized explicitly, so it will default to 0.0f. ?
  • When you call the area() method for c3, it calculates the area using the formula π radius radius. Since the radius is 0.0f, the area will be 0.0f. ??
  • Therefore, the output for the third circle (c3) will be "area of circle = 0.0". ??
  • This setup allows you to create circles with either specific radii or default to a radius of 0.0f. ??

Thanks for reading! ?? Keep learning, keep growing! ?? See you next time! ??

?? Found this post helpful? Have suggestions for improvement? Your feedback shapes future content! Drop a comment or message me. Let's continue this journey together! ??


ROKI ROY

Java, Spring boot, React JS

7 个月

how to build network.

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了