FreeKB - Java Programming Understanding constructors
Java Programming - Understanding constructors

Consider the following markup. In this example, there is a class called MyClass. Within the class, there is a constructor also called MyClass, because the name of the constructor must match the name of the class. Also, a constructor cannot be void (eg. public void MyClass). In this example, the constructor updates the "name" variable to have a value of John Doe. Generally speaking, constructors are used to update a variable to contain a value.

package com.sample.main;

public class MyClass {

  String name;
  public MyClass() {
    name = "John Doe";


As we can see, a constructor exists inside of a bean or class

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter c5b33 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |