answersLogoWhite

0


Best Answer

A constructor is technically a type of method, but it is a very special type. Whereas other methods can be used to do just about anything, the only purpose of a constructor method is to create an instance of the class that contains it, often with parameters passed to it through another part of the program. This instance is called an "object" and is a central part of not only Java, but other object-oriented languages as well. A constructor method always has the same name as its containing class, and does not have a return type. Think of it this way: a class in Java is like a generic blueprint for a house. Your instance variables are like different attributes of the house - how many bathrooms will your house have, what colour will it be? Once you decide on the exact specifications for your house, you can give those parameters to the construction company, which will actually create that house. That's what a constructor method does - takes input parameters (or, lacking them, sets defaults) and creates an object.

User Avatar

Wiki User

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

Wiki User

11y ago

Objects and methods are analagous to reality. Say there was a bike object. It has different methods, or actions, that can be performed. There could be a method to ride, a method to pump the tires, or a method to brake. An object is a noun, and a method is a verb.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A constructor is a special method that has the same name as the class name, and it cannot be invoked or called like a method call, it must be invoked by the new operator. For example, new Object();

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A constructor is usually the first method that gets invoked when a class is instantiated. This method usually creates the class object and sets initial variable values in order for the class object to do its functions.

A Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Two key points to remember about constructors are that they have no return type and their names must exactly match the class name.

Methods can have any names and can have any return types.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Assuming you mean Java: A constructor is very similar to a "normal" method, in that it is a place to type commands. The main differences is that a constructor is invoked automatically every time an object of the specified class is instantiated. With respect to syntax, there are two differences: (1) The constructor must have the same name as the class (including upper- and lowercase), and (2) The constructor has no declared return value, not even void.

This answer is:
User Avatar

User Avatar

Learn bay

Lvl 8
2y ago

Constructor is used to initialize an object whereas method is used to exhibits functionality of an object. Constructors are invoked implicitly whereas methods are invoked explicitly. Constructor does not return any value where the method may/may not return a value.

To learn more about data science please visit- Learnbay.co

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

really there is no difference between constructor overloading and metho overloading

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The differences are:

  • Methods have return types but constructors dont
  • Methods can have any name but constructors name must match exactly with the class name
This answer is:
User Avatar

User Avatar

Wiki User

6y ago

A constructor is a special method that gets executed when the object is created.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between method and constructor in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can a constructor be defined within a method in java?

No.


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.


Why constructor rather than classes in java?

Constructor is not an alternative to class. In Java, you create classes; the classes contain methods - including the constructor, which can be viewed as a special method. If you want to have a constructor, you need a class that surrounds it, so it's not one or the other.


Which is the most usable method or constructor in java?

There is no comparison between methods and constructors. They are both present for a reason and each has its own purpose.


Does Java support copy constructor?

No. Java does not support copy constructor


What is the difference between the constructor to and destructor?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


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 .


Why constructor in Java doesn't have any return type?

The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.


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( ) ").


What is the difference between consructor and function in java?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


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.