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 method and constructors in java ?
difference between this method which takes parameter
int height;
setDim(int height)
{
this.height=height;
}
and the constructor which also does the same
class A
{int height;
public A((int height)
{
this.height=height;
}
}
4 Answers
- Anonymous9 years agoFavorite Answer
Constructors is the place to define the object.
Method is what the object can do or what action the object can do.
For Example:
Car object.
In constructors you should define,
Brand name
Type of car.
Number doors
color of the car
engine type
etc ...
That car object should have methods below;
Drive
SpeedUp
SpeedDown
TurnLeft
TurnRight
Back
Break
etc ...
For your case.
constructors is to set initial value.
method is to change that value later.
- 6 years ago
CONSTRUCTOR:
Constructor is a special member function that has same name as class name and called whenever object of that class is created.
Constructor is used to initialize the object when it is created.
TYPES OF CONSTRUCTOR:
1) Default Constructor
2) Parametrized Constructor
Following are some differences between Constructor and methods:
1) Constructor has same name as class name. method can have any valid name.
2) Constructor is called whenever object is created, but method has to called explicitly with the help of object name.
3) Constructor does not have return type not even void, but method should have return type or void.
4) Constructor overloading is possible but Constructor overriding is not possible
Method overloading & overriding is possible.
For more Details on Constructor Refer following link:
- 9 years ago
typically, constructors are declared private in your object class and are changed by getters and setters,
i have no idea how you declared each method, or if you were just righting in short hand, anyways here is an example of each
public class example(
private static int height;
public example(int height){
this.height = height;//this is a constructor if the local static variable is needed in other methods
}
public void getRecomendedWeight(){//this is a method as it is using privately stored input to do a task that the user has asked for
double recomendedWeight = height /12*mumbo jumbo
}
}