Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
difference between constructor and method?java?
what is the difference between java constructors and methods?what can i use instructors for ?is there something i can do with a constructor but i cant do it with method?or can do with method but cant with constructor?
6 Answers
- CPlusPlus GuruLv 410 years agoFavorite Answer
A constructor is used only once with an object, when you create it, and its code helps initialize its members, and possibly do other things. Methods can be called if and when you need them, an unlimited number of times on the same object.
A constructor is declared like a method, except it has no return type, not even void: public C() { ... }. (Or private, or protected.)
A constructor is not called directly, but used in connection with the "new" operator: C obj=new C().
Source(s): Java and C++ Guru. - deonejuanLv 710 years ago
When I started out I didn't even recognize the constructor of a class, but it has the same name as the class without the word 'class'
public class Car {
// class members
public int numWheels;
// constructor
public Car() {
numWheels = 4;
}
// another constructor
public Car( int total ) {
numWheels = total;
}
OK, you recognize a method() by the parenthesis. method() means branch here, go seek the code with that name and follow directions. When completed, return to the line following the call().
However, constructors have to be called with the java keyword 'new'
Car myCar = new Car();
Car mustang = new Car( 5 );
myCar will use the first constructor. mustang will use the 2nd constructor because it defines one argument to use it.
the 'new' constructs the Object on the computer Heap (RAM). Once we have something in RAM, we can use the dot notation
mustang.numWheels = 6;
If I had made numWheels private
private numWheels;
then, I need 'getters' and 'setters' methods to manipulate or retrieve that value.
the first time Objects made sense to me was a class of StopWatch. Every click of the mouse on the computer screen started a new box with a clock counting up. Each clock was unique with different times displayed counting up. the constructor(s) of those StopWatch class files made Objects. A right-click canceled the clocks one by one -- method(). The Object had a behavior. Which is what an Object can do: values <> states <> behaviors, but ONLY in RAM, not as a class file.
- 6 years ago
Constructor is a special member function which has same name as class name and is invoked when the objects are created.
NEED:
Constructors are needed to initialize the objects when they are created.
TYPES OF CONSTRUCTOR:
1) Default Constructor
2) Parametrized Constructor
Following are some difference between method and constructor:
1) Constructor name is same as class name, method can have any valid name.
2) Constructor is invoked when object is created, but method is called explicitly with the help of object name.
3) Constructor does not have return type not even void, method should have return type or void.
4) We can have constructor overloading not overriding, we can have method overloading and overriding.
For more Details on Constructor Refer following link:
- Anonymous5 years ago
For the best answers, search on this site https://shorturl.im/avAch
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
- How do you think about the answers? You can sign in to vote the answer.
- Anonymous5 years ago
Constructors have the same name as the class and are used when creating new objects of a class. Consider the following: class Point { private int x; private int y; public Point(int xVal, int yVal) { x = xVal; y = yVal; } } // Some time later, say in the main method Point myPoint = new Point(5,4);