answersLogoWhite

0


Best Answer

Constructors have the same name as the class itself and they do not specify a return type, not even void because they return the instance of the class itself.

Because constructors have the same name as the class then they allow method overloading and also save memory and execution time of program.

Program release memory of constructors function after using this function and it reduce program complexity.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

A constructor is a piece of code in a Java class that gets executed when an object of the class is instantiated/created. Some of its special properties are:

  • It does not return anything explicitly. It only creates an object of the class
  • All code related to startup activity of that class goes here
  • If you do not code a constructor, the JVM puts a default no arg constructor here
  • It can take arguments
  • It can be overloaded

Example:

public class Test {

private String name = "";

//No arg constructor

public Test(){

this.name = "Test";

}

//Constructor that has one argument

public Test(String val) {

this.name = val;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this:

class Car {

Car() { } // The constructor for the Car class

}

You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile:

Car f = new Car(); // Won't compile, no matching constructor

but the following will compile:

Car f = new Car("Ford", 43); // No problem. Arguments match

// the Car constructor.

So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods). You can't always make that work for your classes; occasionally you have a class where it makes no sense to create an instance without supplying information to the constructor. A java.awt.Color object, for example, can't be created by calling a no-arg constructor, because that would be like saying to the JVM, "Make me a new Color object, and I really don't care what color it is...." Do you seriously want the JVM making your color choices?

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Constructors are used to create objects of a particular Class.

Overloaded Constructors

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples:

class Car {

Car() { }

Car(String s) { }

}

The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example.

Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name. Here's what it looks like:

1. public class Car {

2. String name;

3. Car(String name) {

4. this.name = name;

5. }

6.

7. Car() {

8. this(makeRandomName());

9. }

10.

11. static String makeRandomName() {

12. int x = (int) (Math.random() * 5);

13. String name = new String[] {"Ferrari", "Lamborghini",

"Rover", "Spyker",

"Lotus"}[x];

14. return name;

15. }

16.

17. public static void main (String [] args) {

18. Car a = new Car();

19. System.out.println(a.name);

20. Car b = new Car("Proton");

21. System.out.println(b.name);

22. }

23. }

Running the code four times produces this output:

% java Car

Lotus

Proton

% java Car

Ferrari

Proton

% java Car

Rover

Proton

% java Car

Ferrari

Proton

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples:

class Car {

Car() { }

Car(String s) { }

}

The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples:

class Car {

Car() { }

Car(String s) { }

}

The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example.

Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name. Here's what it looks like:

1. public class Car {

2. String name;

3. Car(String name) {

4. this.name = name;

5. }

6.

7. Car() {

8. this(makeRandomName());

9. }

10.

11. static String makeRandomName() {

12. int x = (int) (Math.random() * 5);

13. String name = new String[] {"Ferrari", "Lamborghini",

"Rover", "Spyker",

"Lotus"}[x];

14. return name;

15. }

16.

17. public static void main (String [] args) {

18. Car a = new Car();

19. System.out.println(a.name);

20. Car b = new Car("Proton");

21. System.out.println(b.name);

22. }

23. }

Running the code four times produces this output:

% java Car

Lotus

Proton

% java Car

Ferrari

Proton

% java Car

Rover

Proton

% java Car

Ferrari

Proton

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a java constructor and write a program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is there a sample program for constructor?

All Java programs would have a constructor... public class Test { public Test(){ ... } ..... } This is a constructor. Even if you dont code the constructor Java would automatically place a default constructor for compilation.


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


What does it mean in java when a constructor is undefined?

When a constructor is not define in java then the instance used in class is not optimised the value and therefore some times it generates some garbage value. By the way , When we not define a constructor then generally it not distrub the execution of the program.


Does Java support copy constructor?

No. Java does not support copy constructor


What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


Can you use constructor in servlet?

Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them. Note: Even if you write a constructor in a servlet, it will not get executed.


What is the difference between implicit and explicit Java programming?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor. Explicit is opposite to this , ie. programmer has to write .


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


Can a constructor be defined within a method in java?

No.