Skip to main content

Java - Constructors

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up procedures required to create a fully formed object.
All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used.

Syntax

Following is the syntax of a constructor −
class ClassName {
ClassName() {
}
}
Java allows two types of constructors namely −
  • No argument Constructors
  • Parameterized Constructors

No argument Constructors

As the name specifies the no argument constructors of Java does not accept any parameters instead, using these constructors the instance variables of a method will be initialized with fixed values for all objects.

Example

Public class MyClass {
Int num;
MyClass() {
num = 100;
}
}
You would call constructor to initialize objects as follows
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.num + " " + t2.num);
}
}
This would produce the following result
100 100

Parameterized Constructors

Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name.

Example

Here is a simple example that uses a constructor −
// A simple constructor.
class MyClass {
int x;

// Following is the constructor
MyClass(int i ) {
x = i;
}
}
You would call constructor to initialize objects as follows −
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
This would produce the following result −
10 20

Comments

Popular posts from this blog

Spring Security with JWT for REST API

Spring is considered a trusted framework in the Java ecosystem and is widely used. It’s no longer valid to refer to Spring as a framework, as it’s more of an umbrella term that covers various frameworks. One of these frameworks is Spring Security , which is a powerful and customizable authentication and authorization framework. It is considered the de facto standard for securing Spring-based applications. Despite its popularity, I must admit that when it comes to single-page applications , it’s not simple and straightforward to configure. I suspect the reason is that it started more as an MVC application -oriented framework, where webpage rendering happens on the server-side and communication is session-based. If the back end is based on Java and Spring, it makes sense to use Spring Security for authentication/authorization and configure it for stateless communication. While there are a lot of articles explaining how this is done, for me, it was still frustrating to set it up for the f...

How to answer conflict-resolution interview questions

QUESTION 1: How do you deal with conflict? People aren’t going to get along with each other all the time. It’s just a fact. Employers want to know that you can respond to conflict diplomatically, says job-search and interview coach Thea Kelley. If you’re a my-way-or-the-highway type of personality, you’re not going to get very far in the interview. Start off by emphasizing communication and respectfulness as a means to conflict resolution. For example, “I always take the person aside and discuss the issue privately. I listen actively to make sure I understand the other person’s point of view, and I work with the person to develop a solution together.” Stress that even if you both don’t completely agree on the end result, you tried to at least meet each other halfway. Pro tip: “Don’t non verbally communicate resentment when telling a story,” Kelley advises. “Aggressive body language and tone of voice can show that you harbor bad feelings.” QUESTION 2: Tell me about a time when you had ...

E-R Diagrams in DBMS

E-R Diagrams in DBMS: Components, Symbols, And Notations:  E-R diagram is the short form of “Entity-Relationship” diagram. An e-r diagram efficiently shows the relationships between various entities stored in a database. In this article, we will discuss what are ER Diagram, ER Diagrams Symbols, Notations, Their various components like Entity, Attribute and Relationship. E-R Diagrams in DBMS: Components, Symbols, And Notations E-R diagrams are used to model real-world objects like a person, a car, a company etc. and the relation between these real-world objects. An e-r diagram has following features: E-R diagrams are used to represent E-R model in a database, which makes them easy to be converted into relations (tables). E-R diagrams provide the purpose of real-world modeling of objects which makes them intently useful. E-R diagrams require no technical knowledge & no hardware support. These diagrams are very easy to understand and easy to create even by a naive user. It gives a...