Ask coding questions

← Back to all posts
Adding constrictors to code
tikis (2)

How do I add Constrictors in this code?

Answered by Zavexeon (1033) [earned 5 cycles]
View Answer
Zavexeon (1033)

A constructor is formatted like this:

public ConstructerName(// arguments here) {
   // your code here

For example:

public Dog(String breed, String name) {
    dogBreed = breed; 
    dogName = name;

// initializing a new instance 
Dog stormy = new Dog("pitbull", "Stormy");
// called like ConstructerName variableName = new ConstructerName(// params here);

So in the above example it creates a new instance of the Dog constructor stored in the variable stormy with the strings pitbull as the breed and Stormy as the name.

Since everybody else does this, I'll do it too. Please upvote my comment and mark it as the answer if it helped. ^w^