What exactly the work of constructor( default, parameterized) especially about instance variables initialization. Suppose user initialize the instance variables explicitly. Then what is the use of parameterized constructor over here.. Please reply me

Asked by Last Modified  

17 Answers

Learn Java

Follow 0
Answer

Please enter your answer

Computer Tutor

Constructor is used to initialize an object at the time of creation. A default constructor is provided by the compiler itself if no constructor is provided by the user. It initializes the state of the object to its default values. Parameterized constructor is used to provide values to the object at the...
read more
Constructor is used to initialize an object at the time of creation. A default constructor is provided by the compiler itself if no constructor is provided by the user. It initializes the state of the object to its default values. Parameterized constructor is used to provide values to the object at the time of creation. If you want to set the values later, after the creation of the object then one can use setter methods to set values. read less
Comments

Software Developer, Expertise in Java/J2ee Technology.

Construct as the name suggest are used to construct the object. So, they are invoked only once and that too at the time of object creation. Default Constructor : Initialize all the instance variables with their default values. Parameterized Constructor : Are used to initialize the instance variables...
read more
Construct as the name suggest are used to construct the object. So, they are invoked only once and that too at the time of object creation. Default Constructor : Initialize all the instance variables with their default values. Parameterized Constructor : Are used to initialize the instance variables at the time of object creation. 1. There are scenarios where the values of instance variables are known at the time of object creation, so in these scenarios parameterized constructors helps to avoid explicitly initialization of instance variable. 2. There are scenarios where the values of instance variables are not known at the time of object creation, in those scenarios, explicit initialization may be needed on basis of application requirement. read less
Comments

Software Developer, Expertise in Java/J2ee Technology.

Exact work of constructor Or Construction of Object :- It allocates the memory for the object in heap memory. It counts the number of bytes of memory by adding all the instance variable memory sizes. (For eg : int 4 bytes, byte 1 byte etc).
Comments

Constructor is mainly used to access the non static members inside the class by using object and to initialize the class members.
Comments

Senior Software Engineer

Well constructor's is used for object creation and initialization. Yes we can explicitly initialize the instance variable, but as i think it will take one more extra statement to execute. If you are creating one or two objects it is okay, but how about array of objects? So it is good to use Parameterized...
read more
Well constructor's is used for object creation and initialization. Yes we can explicitly initialize the instance variable, but as i think it will take one more extra statement to execute. If you are creating one or two objects it is okay, but how about array of objects? So it is good to use Parameterized constructor if you want to initialize values while object creation. read less
Comments

Java is primarily an object oriented programming language..that means there will be objects ..objects will have state and behavior..state points to the values of the attributes and the behavior is the methods that we give inside an object..having said that..when make a parameterized constructor we are...
read more
Java is primarily an object oriented programming language..that means there will be objects ..objects will have state and behavior..state points to the values of the attributes and the behavior is the methods that we give inside an object..having said that..when make a parameterized constructor we are assigning some sort of a default state for that object eg: class Person { private String name; private int age; public Person(String argName,int argAge){ this.name = argName; this.age = argAge } public String toString(){ System.out.println("Name" + this.name); System.out.println("Age"+this.age); } public static void main(String[] args){ Person sidd = new Person("Sidd",29); Person sur = new Person("Sur",21); sidd.toString();//will show Sidd and 29 sur.toString();//will show Sur and 21 } } so here we have 2 objects with two different states..but if we did this then class Person{ String name = "sameNameAlways"; int age = 39; public Person(){ //dont do anything here... } public String toString(){ System.out.println("Name" + this.name); System.out.println("Age"+this.age); } public static void main(String[] args){ Person sidd = new Person(); Person sur = new Person(); sidd.toString();//will show sameNAmeAlways and 39 sur.toString();//will show sameNAmeAlways and 39 } } this will have 2 different objects because we used the new operator but with same states..redundant or not of any use or duplicate objects... read less
Comments

Computer Savvy Professional

A constructor call is more complicated than an ordinary subroutine or function call. It is helpful to understand the exact steps that the computer goes through to execute a constructor call: First, the computer gets a block of unused memory in the heap, large enough to hold an object of the specified...
read more
A constructor call is more complicated than an ordinary subroutine or function call. It is helpful to understand the exact steps that the computer goes through to execute a constructor call: First, the computer gets a block of unused memory in the heap, large enough to hold an object of the specified type. It initializes the instance variables of the object. If the declaration of an instance variable specifies an initial value, then that value is computed and stored in the instance variable. Otherwise, the default initial value is used. The actual parameters in the constructor, if any, are evaluated, and the values are assigned to the formal parameters of the constructor. The statements in the body of the constructor, if any, are executed. A reference to the object is returned as the value of the constructor call. The end result of this is that you have a reference to a newly constructed object. read less
Comments

ICT provide computer education such as a Engineering,School Subjects, Graphics & Web Design

Here, difference between how to object created.
Comments

Java is primarily an object oriented programming language..that means there will be objects ..objects will have state and behavior..state points to the values of the attributes and the behavior is the methods that we give inside an object..having said that..when make a parameterized constructor we are...
read more
Java is primarily an object oriented programming language..that means there will be objects ..objects will have state and behavior..state points to the values of the attributes and the behavior is the methods that we give inside an object..having said that..when make a parameterized constructor we are assigning some sort of a default state for that object eg: class Person { private String name; private int age; public Person(String argName,int argAge){ this.name = argName; this.age = argAge } public String toString(){ System.out.println("Name" + this.name); System.out.println("Age"+this.age); } public static void main(String[] args){ Person sidd = new Person("Sidd",29); Person sur = new Person("Sur",21); sidd.toString();//will show Sidd and 29 sur.toString();//will show Sur and 21 } } so here we have 2 objects with two different states..but if we did this then class Person{ String name = "sameNameAlways"; int age = 39; public Person(){ //dont do anything here... } public String toString(){ System.out.println("Name" + this.name); System.out.println("Age"+this.age); } public static void main(String[] args){ Person sidd = new Person(); Person sur = new Person(); sidd.toString();//will show sameNAmeAlways and 39 sur.toString();//will show sameNAmeAlways and 39 } } this will have 2 different objects because we used the new operator but with same states..reduntant or not of any use or duplicate objects... read less
Comments

Java Technical Leader

Look to be specific parametrized constructor are useful for populate those instances which are must/or required for a object. If a particular instance vairable value is optional you can set it via setter. It is purly design decision
Comments

View 15 more Answers

Related Questions

What is the advantage of Java?
Java offers several advantages, including platform independence, strong community support, robust security features, and a wide range of libraries and frameworks for application development. Its "Write...
Kripal
0 0
5

I have been working in a medical billing company for five years. I want to switch my carrier from non-IT to IT. I have only a BA degree, but I have good knowledge of computers. I know some basics of HTML and manual testing; can I learn java selenium directly to get a job in automation?

Hi Sirajudeen.. Java selenium is one of the good options.. one more easy way to shift to IT is learning Oracle SQL.. Scope of Oracle SQL always stands high since the database is backbone of all developement,...
Sirajudeen

How to prepare for CORE JAVA for Android Developement?

Core Java is required for basic understanding of inheritance , AWT with button action programs, library usage etc then after u can genrate your logic with additional topics of Android
Rishabh
0 0
6
As a fresher I have done Python training, but hardly getting response from any company. What should I do?
only having training in Python is not enough to be called for intervew. Better you should get real time certification course form reputed organisation with smart hand-on real project exposure. Try to have...
Yamini
0 0
6

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Java8 Filters and collectors
Lets say we have collection of strings and we would like to filter (remove) out certain strings from collection. We could achive the same in java 7 and earlier versions import java.util.ArrayList; import...

Spring - Dependency Injection (DI)
Spring - Dependency Injection (DI) DI is a framework which provides loose coupling in code. Here loose coupling means no hard coding of the object. Instead of hard coding, we will be injecting these object...

1.1. Reverse an array in Java.
public class Main { public static void main(String args) { int arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length / 2; i++) { int temp = arr; arr = arr; arr = temp; } System.out.println(Arrays.toString(arr)); }}

Example of DependsOnMethod in TestNG
public class dependsonM { @Test public void login() { System.out.println("login"); } @Test (dependsOnMethods = {"login"}) public void email() { //Intentionally I am failing this testcase Assert.assertTrue(false);...
S

Sarthak C.

0 0
0

Lambda Expressions in Java
A lambda expression, introduced in Java 8 is similar to a block of code resembling an anonymous function that can be passed to constructors or methods for execution as an argument. Examples: a)() ->...

Recommended Articles

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you