Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // avoiding telescoping constructors using a non-static block
- public class Test {
- int x;
- int y;
- { // put common code here - all constructors execute this..
- System.out.println("In non-static block, doing common code");
- x = 4;
- }
- Test() {
- System.out.println("in no-arg constructor");
- y = 5; // no-arg ctor makes y 5
- }
- Test(int val) {
- System.out.println("In int arg constructor");
- y = val; // this makes y whatever we want
- }
- public static void main(String[] d) {
- Test t1 = new Test(); // x=4 y=5
- Test t2 = new Test(6);// x=4 y=6
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment