BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Java Constructor - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Default Constructor

A constructor looks like a method except that it does not specify a return type. Its name must be exactly the name of the class that it constructs.

Actually, every class in Java has constructor. If no other constructor is specified, the Java compiler automatically creates a constructor with no arguments. If a constructor does not explicitly call superclass constructor, the compiler will automatically add an implicit call to the superclass no-arg constructor as the very first statement as we see the code below:

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();
	}
}




Constructor Initialization

Before we investigate the order of initialization of a class which has constructor, static data, and not-static instance variable, let's briefly look at the constructor.

To create an object, we need three steps.

  1. Declare a reference variable
    Make a new reference variable of a class or interface type. myCat is a Cat reference
    Cat myCat = new Cat();
  2. Create an object

    Cat myCat = new Cat();

  3. Link the object and the reference.
    Assign the new object to the reference.

    Cat myCat = new Cat();

When we use "new Cat();", we're not calling a method but a constructor.
It's the code that runs when we use new.
In other words, constructor is the code that runs when we instantiate an object.
Every class we create has a constructor, even if we don't write it ourselves.

Let's look at the following code and check what's happening when we do

	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:

  1. Cat constructor is invoked. Every constructor invokes the constructor of its superclass with an implicit call to super().
  2. Animal, superclass of Cat, constructor is invoked.
  3. Object constructor invoked.
  4. Object instance variables are given their explicit values.
  5. Object constructor completes.
  6. Animal instance variables are given their values if any.
  7. Animal constructor completes.
  8. Cat instance variables are given their values if any.
  9. Cat constructor completes.



Initialization Order

The order of initialization is determined by the order the appearance of the variable definition with a class.
In the following example, we'll find the order of initialization is:

  1. Instance variable
  2. Constructor
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().



Initialization Order with Static Storage

From the following example, we'll learn:

  1. Static initialization occurs only if it's necessary.
  2. Static objects are not reinitialized.
  3. The order of initialization is statics first, if they haven't already been initialized by a previous object creation, and then the non-static objects.
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() constructor

To 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.

Then, in the main(), we load Dog class which has static field BabyDog sBaby2. After the static field it goes to regular field BabyDog sBaby3 and then construct Dog object. In the second creation of Dog from main(), it skips static sBaby2 and goes to sBaby3.







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master