1class Other{
2 public Other(String message){
3 System.out.println(message);
4 }
5}
6
7class scratch{
8 public static void main(String[] args) {
9 Other method = new Other("Hey");
10 //prints Hey to the console
11 }
12}
1A constructor is a special method used to initialize objects in java.
2we use constructors to initialize all variables in the class
3when an object is created. As and when an object
4is created it is initialized automatically with the help of
5constructor in java.
6
7We have two types of constructors:
8Default Constructor
9Parameterized Constructor
10
11public classname(){
12}
13public classname(parameters list){
14}
1A constructor is a special method
2used to initialize objects in java.
3we use constructors to initialize
4all variables in the class
5when an object is created.
6 As and when an object
7is created it is initialized
8automatically with the help of
9constructor in java.
10
11We have two types of constructors:
12Default Constructor
13Parameterized Constructor
14
15I use Constructor in pages classes in my framework
16public classname(){
17}
18public classname(parameters list){
19}
20
21
1public class ConstructorDemo
2{
3 int a;
4 ConstructorDemo()
5 {
6 a = 26;
7 }
8 public static void main(String[] args)
9 {
10 ConstructorDemo obj = new ConstructorDemo();
11 System.out.println(obj.a);
12 }
13}