Why do I need to define custom constructor? - java

If I have a class, let's say, extended from DialogFragment and define custom constructor for it, why should I define a default one? If I wouldn't I get the error message if runtime change occurs.

I suspect the problem is that in Java, the compiler creates a parameterless constructor for you unless you specify one yourself. If something within Android requires a parameterless constructor, then either you need to not declare any constructors yourself or you need to explicitly declare a parameterless one.
From section 8.8.9 of the Java Language Spec:
If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:
If the class being declared is the primordial class Object, then the default constructor has an empty body.
Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.
Does that make things clear? I don't know enough about Android to know why you need a parameterless constructor, but presumably it's so that instances can be created via reflection without specifying any arguments for constructor parameters.

Constructors may contain code that is run when the object is created. It is sort of like setup code that you want done so the object is ready for what it's supposed to do.

Related

LinkedList data structure, why we need to call empty default constructor from a different constructor

In java.util.LinkedList class, there are 2 constructors defined, default and parameterized that accepts a collection. When I looked at the implementation, I see that the parameterized constructor has this(), basically is calling the default constructor.
But the default constructor is empty. So I was just curious if this is a best practice to call default constructor even if its empty or is there any reasoning behind this. What happens if we don't call default constructor.
GrepCode LinkedList implementation
If the default constructor is extended at any time in the future, the other constructor does not have to be changed.
It tells us, that the extended constructor is basically a default constructor, but with some added special functionality (for convenience).
It simply means any form on initialization of LinkedList must perform default initialization steps first.
It might seem weird to you as there are no default initialization steps as of now (but might come up later).
It's not really a recognized good practice. Maybe the default constructor did something in previous versions. Maybe the developer thought it would be a good idea, so that if something additional is done in the default constructor, it will also be done in the other one. If you look at ArrayList, you'll see that a different choice was made.
What happens if we don't call default constructor.
The default constructor is just not called. The superclass no-arg constructor is called implicitely.
The parametrized version should create a list the same way as not parametrized, and call a method to add elements additionally. The fact that default constructor is empty is implementation specific.
/**
* Constructs an empty list.
*/
public LinkedList() {
}
The fact that it's intended to construct it this way remains.

Need Clarity on Java constructors

Okay am just beginning to learn Java and I have a doubt regarding constructors.
isn't constructor just another method with same name as class?? If so Why do we need it? Cant we just use methods?
Constructors have no return type.
They are used for instantiating a class.
They can be called from subclass using Super keyword.
Their name must be same as it's classname
Constructors are called on object creation. You can initialize stuff in other methods of course but then you'd have to call them explicitly.
You don't need to write a constructor. But if you want to do things to ready your object, you put in in your constructor.

Javadoc identifying superclass constructor

If I go to the Javadoc page for the RandomGenerator class I see a summary of the class constructor and methods. It says public class RandomGenerator extends Random.
How can I find out more information about how RandomGenator class invokes the constructor of Random class? I don't see any references in RandomGenerator class to it's superclass constructor.
Most likely you have to contact authors. For some reason they decided not to include this information into javadoc (public contract).
You may also look into class sources or use java decompiler if you don't have sources.
In case class constructor doesn't call super() directly that means that superclass has no-argument constructor which call is the first line of your class constructor always, and you do not need to call it explicitly.
P. S. My answer has two different approaches. I apologize for that, I just not quite sure whether you have sources for analysis.
The short answer is, you can't from the javadocs directly.
You can look at the parent class, find that it has 2 constructors, guess which one is being called based on the parameters the child class constructor exposes.
And ofcourse the most conclusive answer can be given by looking at the source code if available or decompiling the class if it's not.
From the documentation:
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
Therefore, they just decided not to actually write the call to the super constructor and use the default one.

Why the necessity of having a public empty constructor in every Fragment?

Recently I came across the statement in the Fragment docs:
All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore.
In this regard, I have already read the following posts:
1. Explicitly writing default empty constructor.
2. Default constructors and inheritance in Java.
3. Java entity - why do I need an empty constructor?.
4. Do fragments really need an empty constructor?.
If I recall correctly, Java automatically provides a default empty constructor when no constructor is defined (as is the case with Fragments), so why do we need to define one anyway ? What purpose does this serve ? And what difference does it make if we don't define a default constructor in a Fragment ?
Java automatically provides a default empty constructor when no constructor is defined (as is the case with Fragments)
Yes it does, but if you have a parametric constructor then it doesn't.
But in the case of fragments, the FragmentManagerImpl recreates then with reflection using the default constructor after process death (low memory condition), so any parameters you would provide instead of using the arguments bundle will be nulled out.
If you do not define any constructors you will have the default public empty constructor and it is just what you need. No need to define it explicitly. But if you create a non-empty constructor (which is not a widely used practice) you should also always define public empty constructor that will be used by the system when the fragment will be recreated.

Override constructor in all subclasses IntelliJ

I wanted to know if there is possibility to generate constructors for all subclasses in IntelliJ.
I mean if i have many subclasses and somewhere in development stage I wanted to add new constructor, with a flag, for example. Is there a way to generate this constructor in all subclasses too?
I wanted to know if there is possibility to generate constructors for all
subclasses in IntelliJ?
The answer would be no and one of the reason would because it is design specific.
When you create a subclass and define a non default constructor in it you have the liberty to call any one of the overloaded superclass constructor from it. Which one would be design choice as initialization of superclass variables may depend on it. Never the less call to one of the overloaded super class constructor must be 1st statement of subclass constructor. If you do not provide any, call to default constructor of super class is inserted by default by the JVM. However in that case if your super class does not have default constructor(you have explicitly provided some parameterized constructor) then compiler error will occur.

Categories

Resources