0
How we can identify method and constructor in a java program..?
Can u explain briefly with a example program..?
3 Réponses
+ 3
Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
This is a constructor:
public Bicycle(int startCadence, int startSpeed, int
startGear)
{
gear = startGear;
cadence = startCadence;
speed = startSpeed; }
This is a method:
public int Sum(int a, int b) {
int result;
result=a+b;
return result;
}
+ 2
Method:Its the behavior or actions of someone or something like barking of a dog,running of cheetah
public class Dog{
public void run(){
System.out.println("running");
}
}
Whereas Constructor is a method with same class name and is called whenever object of it has been created and you can create its multiple version to suit your needs.
Following is the class of Bankaccount with its object
public class Bankaccount{
Bankaccount persona=new Bankaccount();
}
Now if we want to store particular amount into bank account in start(when object is created) we can do like
public class Bankaccount{
int startcash;
public Bankaccount(int money){
startcash=money;
}
}
"This" refers to current object
i removed it cause i wrote it on the other sides you can do without this
0
Thank you..Then what is use of 'this' keyword in the above program..?