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.
Related
We know 'interface' in java provide a common way to access objects who implementing it, but i wonder if there's a way like interface which not just for accessing the object's method, but also for accessing the class's method(static method). I want to use it to invoke an array of different classes's static factory method. Does java provide something like it?
No.
But you can implement the pattern you describe easily with an ordinary object interface, then a set of classes that just wrap the static methods you wish to call through to.
If your intentions is to call static method of an implementing class using Interface reference, the answer is No. static members belong to class only, so you will always need a Class type reference (actually it should be the class name) to access them.
With interfaces you can only point to what you have declared in it.
No, there's no kind of "interface for static methods" in Java.
(Aside from anything else, how would you expect to specify the class in question? Even generics wouldn't help here due to type erasure.)
I have a package which has some N number of classes and I'm scanning all the classes and initializing them through a method. All the classes with a default constructor are being initialized but the ones without default(zero argument) constructor throws an exception. Does anyone know how to create an object without default constructor?
P.S. I need a java code.
Use Class#getConstructors() to find a defined constructor, and call that instead.
You can try looking here. It explains how to create objects using Java reflection.
Or just Google: java constructor reflection. I got this one using the "I'm feeling lucky" feature
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.
What is the advantage of having constructor simillar to the one mentioned below:-
class A{
public A(A a){}
public A(){}
}
If you mean the one with the parameter, there's no reason for having that at all, given that it completely ignores the parameter, and there's already another constructor with the same effect.
If you can give a more realistic example, we may be able to give more useful information...
A(A a){/*do something*/} Can be helpful as copy constructor.
As others have said, you have a copy constructor. There are a number of reasons why you may want a copy constructor. Some of those are:
You can provide an alternative to the clone method. (Which is implemented via the Clonable interface.)
Copy constructors are easily implemented.
You can use another constructor to build the copy (by extracting data from the original object and forwarding to a regular constructor).
Check out the link I added to this post for more information about copy constructors and why you would want to use them (if you need them).
There is no advantage unless you need to have a copy constructor. I would suggest using the clone() method if this object should be clonable rather than using a copy constructor semantic.
You're question is very unclear, but basically if you hava a class, that has a constructor, that takes an instance of the same class, then you have a copy constructor. i.e. a constructor that creates a new instance with the same internal values as the original.
Edit -- assuming of course that your constructor does something other than just create a new instance.
It could also be useful in a number of delegation-based design patterns such as decorator or proxy etc.
Providing a default constructor might still be considered good practice, especially in scenarios where dependency injection or serialization are considered.
I have an object that I instantiate - it's quite nifty as it also extends a superclass and does some stuff in the constructor - in fact all the vital parameters and method calls are handled in the constructor.
After this I never call the object again to do something - I don't pass it to any other objects either - after it's instantiated it does it stuff and all is good. Is this the wrong way to do it?
Yes, doing significant work in the constructor is usually a bad idea.
Could you do this through a static method instead? The static method could create an instance of the superclass and then do whatever it needs to. The only problem with this approach would be if the superclass called virtual methods during its constructor, but that's a code smell in itself...
It may be a smell from the superclass, not the subclass.
Why do you need to do that? Is there functionality in the superclass that is publicly accessible only through the constructor? If so then you probably do need to create an instance to access that functionality. But it may be better to hide that behaviour behind a normal method rather than a subclass constructor.
But that's still not as good as fixing the superclass.
There should a purpose for the object to exist. Constructor is only a tool for preparing the object so it can work as needed after creation. If you don't use the object, you don't need to create it either.
Creation of unnecessary objects consumes memory and also makes garbage collector do more work, so you should consider rewriting code (possibly with a static method as it has already been suggested).