Java Class Constructor


#1

Is the class constructor always going to be
named after the class, or is it just for an example?

class Dog {

   public Dog() {

  }
}

#2

The constructor will always be the same as the class name.

Some examples:

class Test {
  public Test() {}
}

class ThisIsALongClassName {
  public ThisIsALongClassName() {}
}

Note however, the scope modifier, public in front of it will change depending on the access the programmer wants other classes to have on said class.

no modifier (Dog()): package-level; classes in same package can instantiate
public modifier (public Dog()): any class can instantiate, even outside of the current project
private modifier (private Dog()): no class can instantiate it, it can only be referred to


#3

Okay, appreciate that Jacob.


#4

I edited a bit, to clarify.