answersLogoWhite

0

Example of constructor in java

Updated: 12/23/2022
User Avatar

Wiki User

12y ago

Best Answer

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this:

class Car {

Car() { } // The constructor for the Car class

}

You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile:

Car f = new Car(); // Won't compile, no matching constructor

but the following will compile:

Car f = new Car("Ford", 43); // No problem. Arguments match

// the Car constructor.

So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods)

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Example of constructor in java
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does Java support copy constructor?

No. Java does not support copy constructor


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


Is there a sample program for constructor?

All Java programs would have a constructor... public class Test { public Test(){ ... } ..... } This is a constructor. Even if you dont code the constructor Java would automatically place a default constructor for compilation.


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


Can you use business logic in constructor with example in java?

No. Logic should never go in a constructor; constructors should only be used to instantiate and initialize object data.


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


Can a constructor be defined within a method in java?

No.


What is copy constructor in java?

Java, unlike C++ does not support copy constructors.


Can you create a constructor for an interface in java?

NO, we cannot create a contructor for an interface in java.


Why constructor rather than classes in java?

Constructor is not an alternative to class. In Java, you create classes; the classes contain methods - including the constructor, which can be viewed as a special method. If you want to have a constructor, you need a class that surrounds it, so it's not one or the other.


What is the difference between implicit and explicit Java programming?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor. Explicit is opposite to this , ie. programmer has to write .