-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstructors.java
More file actions
34 lines (31 loc) · 1.22 KB
/
constructors.java
File metadata and controls
34 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package constructor;
import methods.ObjMethod;
// driver class for the constructor demo.
public class constructors {
public static void main(String[] args) {
// calling default constructor.
constructorDemo cd1 = new constructorDemo();
cd1.showData();
// calling parameterized constructors
constructorDemo cd2 = new constructorDemo(2);
cd2.showData();
constructorDemo cd3 = new constructorDemo("parametrised constructor 2");
cd3.showData();
constructorDemo cd4 = new constructorDemo(4, "parametrised constructor 3");
cd4.showData();
// demo of constructor chaining.
constructorChain cc = new constructorChain();
System.out.println("cc.x= " + cc.x + "\tcc.y= " + cc.y);
/*
* private constructors can not be used outside the declaring class hence such
* classes can not be instantiated. data members needs to be static for such
* classes and are known as utility classes.
* PvtConstructor pv = new PvtConstructor(); //can not create object of class with private constructor.
*/
// accessing static data member of the utility class with a private constructor.
System.out.println(PvtConstructor.val);
// from ObjMethod.java in package methods;
ObjMethod om = new ObjMethod();
om.sayHello();
}
}