on
Italy
- Get link
- X
- Other Apps
public class MyClass {
public void out() {
System.out.println("MyClass");
}
// compiler will create this constructor if not defined
public MyClass() {
super();
}
}
public class DefaultConstructorTest {
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.out();
}
}
In the code below, because the MyClass class explicitly defines a constructor, Java compiler does not implicitly add a default. That means that trying to create a MyClass, with no arguments, will cause an error:public class MyClass { public void out() { System.out.println("MyClass"); } public MyClass(int a) {} } public class DefaultConstructorTest { public static void main(String[] args) { MyClass mc = new MyClass(); // error: The constructor MyClass() is undefined MyClass mc2 = new MyClass(10); // OK mc.out(); } }
Cat myCat = new Cat();Complete code looks like this:
public class Cat extends Animal{ void makeSound() { System.out.println("Mew!"); } } public class Animal {} public class ConstructorTest { public static void main(String[] args) { Cat c = new Cat(); c.makeSound(); } }Here are the things happening at runtime when the Cat constructor is invoked:
class Door { Door (int doorNum) { System.out.println("Door(" + doorNum + ")"); } } class Room { Room() { System.out.println("Room()"); d2 = new Door(22); } Door d1 = new Door(1); void floor() { System.out.println("floor()"); } Door d2 = new Door(2); } class InitOrder { public static void main (String[] args){ Room r = new Room(); r.floor(); } }Output is:
Door(1) Door(2) Room() Door(22) floor()When we instantiate the Room at:
Room r = new Room();the code does not go into the constructor Room(). Instead it initializes the instance variable d1 and d2. To do that, it needs to make objects of type Door. Then, it assigns the new objects to the reference variables, d1 and d2. Then it goes to the constructor Room().
class StrangeDog { StrangeDog(){ System.out.println("Strange Dog()"); } static BabyDog sBaby3 = new BabyDog(3); } class NeighborDog { NeighborDog() { System.out.println("NeighborDog()"); } static BabyDog bDog = new BabyDog(1); } class BabyDog { BabyDog(int numBabyDog){ System.out.println("BabyDog(" + numBabyDog +")"); } } class Dog { BabyDog sBaby3 = new BabyDog(3); Dog (){ System.out.println("Dog() constructor"); } static BabyDog sBaby2= new BabyDog(2); } public class DogInit { public static void main (String[] args) { System.out.println("1: Creating Dog() in main()"); new Dog(); System.out.println("2: Creating Dog() in main()"); new Dog(); } static NeighborDog nbDog = new NeighborDog(); }The output is:
BabyDog(1) NeighborDog() 1: Creating Dog() in main() BabyDog(2) BabyDog(3) Dog() constructor 2: Creating Dog() in main() BabyDog(3) Dog() constructorTo execute main() which is a static method, the DogInit class must be loaded, and its static fields nbDog is then initialized, which causes NeighborDog class to be loaded. Since the NeighborDog class contains static BabyDog object, BabyDog is then loaded. So, the BabyDog and NeighborDog classes get loaded before main() starts.
Comments
Post a Comment