jo pizzamaker
jo pizzamaker

Reputation: 83

constructor clarification

Given this code:

public class Example
{

public Example(String name)
  {
    input = name;
  }

public someMethod()
  {
    //some code
  }
}

In java when you declare a new (object or instance) as such:

Example foo = new Example("jo");

what is actually happening here? my question is:

  1. is a new object being created with the parameter of the class constructor?

  2. will the new object have all of the arguments of the constructor and data members within the braces?

Upvotes: 0

Views: 180

Answers (3)

Lews Therin
Lews Therin

Reputation: 10995

As Rohit said you will have a compiler error as no default ctor is specified.

However, assuming you did have a default ctor: what happens is the data members are created, that is, memory is allocated for each non static instance member.

If the class extends any class, the constructor (parent's ctor) should be invoked before the subclass's constructor body executes, and then the constructor body initializes data members using local variables or local variables that are passed as ctor arguments.

If the constructor body executes to a completion, that is no exceptions, it returns a reference to the newly created object.

So:

  1. new creates the object and returns the reference of that object if everything went well.

  2. Constructors only initialize data. Using data defined in the constructor body, or from arguments supplied by the constructor caller. Those arguments don't create an object, they supply data that may be useful to an object.

Upvotes: 1

Andrew Martin
Andrew Martin

Reputation: 5761

This will not compile. Java will automatically define a constructor for you if you don't include one. It will not include parameters. So if you took the public Example piece out of your code, or added in a no argument constructor Java would compile it.

Your code won't compile as you created a constructor and then tried to instantiate an object of the class without giving it any attributes (causing a compile error).

Additionally, it is worth pointing out that your code wouldn't work anyway as the input you use isn't defined anywhere. It should be defined before the public Example like so:

    String input;

If you did this and DIDN'T specify a constructor (i.e. let Java create it for you), Java would hold the value "null" for input.

Any instance variables that are declared public and belong to the class will be accessible by the object you create.

Edit: Now you have altered your code to include the passing of an argument, it will compile provided you declare the String input as in my answer in the class.

Upvotes: 1

iTech
iTech

Reputation: 18460

Given your code that does not have a default constructor

the line Example foo = new Example(); will result in compilation error

The default constructor is defined by default unless you add another constructor with argument(s). In this case you have to explicitly define the default constructor otherwise you can only create instance with the constructor that takes argument(s)

Upvotes: 1

Related Questions