MasterWali
MasterWali

Reputation: 31

OOP - Are constructors required?

So I was watching a youtube video and the youtuber said: "When you are creating 'this' object, you are going to need to set it to a new ' type ' of this object"...

The class was called objectIntro and the constructor was:

                public objectIntro(){
//Object Constructor (Method)
}

So here's my question...

I tried to create an object which basically tells me about the level of petrol with in a car...

public class car {

double petrolLevel;
double tankSize;

public void refillPetrol(double I){

    if(I>tankSize){
        I = tankSize;
        petrolLevel = petrolLevel + I;
    }
    else{
        petrolLevel = petrolLevel + I;
    }
}

public void fuelConsumption(double O){

    if(O>tankSize){
        O=tankSize;
        petrolLevel = petrolLevel - O;
    }
    else{
        petrolLevel = petrolLevel - O;
    }
}

public String returnPetrolLevel(){
    return String.format("%sL", petrolLevel);
}

}

Then the class in which the object is created is...

public class carObject {
public static void main(String[] args){

car object1 = new car();


object1.tankSize = 50;//Litres
object1.petrolLevel = 0;    

object1.refillPetrol(50);
object1.fuelConsumption(20);
object1.returnPetrolLevel();

System.out.printf("Petrol Level: %s", object1.returnPetrolLevel());
}
}

My question is, how come my object works without a constructor? In the car class, I do not have a method which says "public car(){ }", whereas the youtuber stated this would be required?

Could someone clear this up, also I think I am not using the term constructor and method in the write context, could someone explain the definition of these terms, along with some examples.

Thanks

Upvotes: 0

Views: 262

Answers (5)

Vickal
Vickal

Reputation: 161

The answer to your question is, if you do not provide a constructor to a class the JVM will implicitly call the default constructor which has no parameter.

For detailed information about constructor you can refer below link https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Upvotes: 1

Sathya
Sathya

Reputation: 9

Even if you do not explicitly create a constructor in the class, a default constructor will be created during compile time and used.

https://msdn.microsoft.com/en-us/library/aa645608(v=vs.71).aspx

Upvotes: 0

MaxZoom
MaxZoom

Reputation: 7753

JLS takes a special care of a class without constructor:

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

This means that if you don't write any constructor for your class, one will be provided for you by the compiler and all class members will be initialized to default values by Java Virtual Machine.

But once you write a constructor, only that one will be used to create a class instance.

This behavior is usually good for a class that serves as a data structure, while normal class will have some constructor defined with default initialization code.

Upvotes: 1

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

It's all in the Java tutorial

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors.

Also the convention is to uppercase your class names, lowercase your method parameters and use getters/setters for member variables which usually are private.

Sometimes you actually might notice that you cannot do new MyClass() or well that you cannot instantiate and object with new at all. This sometimes happens because the coder provided a no-arg private constructor. This is done when for instance you want the user to instantiate the object using a factory method (that you provide in that class) etc. But still doesn't change the fact that the constructor has to be there (that's part of the language spec).

If you want to know more about the default constructor you can consult the java language spec.

Upvotes: 4

missimer
missimer

Reputation: 4079

If you do not have a constructor there is an implicit constructor that sets all the members to their default value, e.g. 0 for int

The difference between a constructor and a method is the constructor creates and initializes an object while a method is for an object that already exists. You can think of a constructor as a function that is being called on your newly created object to initialize the data in some way.

Upvotes: 1

Related Questions