Good day to all my friends.Today I would like to say a important Opp concept.That is constructor.
Why constructors....
Because it is simple and concise.
constructor has got following features.
1. It has got the same name as the class name.
2. It has no return data type.Even viod.
3. This is because they return the instance of the class itself.
following example will lead to a good way to learn about constructor.Run this and have the output as you do in normal java programe.
Read the comments I have given.
class studentimp{
String name;
studentimp(String name){ //this is the constructor and it has no return data type even void.
this.name=name;
}
String getName(){
return name;
}
}
class student{
public static void main(String args[]){
studentimp student1=new studentimp("Manuja");//This is how we instantiate a constructor.
System.out.println(student1.getName());//This will pront the name.
}
}
The below show how to run the program.Save the above code in one java file and run as a normal java file.
Output :
Why constructors....
Because it is simple and concise.
constructor has got following features.
1. It has got the same name as the class name.
2. It has no return data type.Even viod.
3. This is because they return the instance of the class itself.
following example will lead to a good way to learn about constructor.Run this and have the output as you do in normal java programe.
Read the comments I have given.
class studentimp{
String name;
studentimp(String name){ //this is the constructor and it has no return data type even void.
this.name=name;
}
String getName(){
return name;
}
}
class student{
public static void main(String args[]){
studentimp student1=new studentimp("Manuja");//This is how we instantiate a constructor.
System.out.println(student1.getName());//This will pront the name.
}
}
The below show how to run the program.Save the above code in one java file and run as a normal java file.
Output :
Here we use the class student to compile as it is the class that consist the main method...
Now it over..Be happy...
-Ujitha Manuja Sudasingha-
Now it over..Be happy...
-Ujitha Manuja Sudasingha-