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.
No. Logic should never go in a constructor; constructors should only be used to instantiate and initialize object data.
The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.
Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.
You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class. While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior. For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage. For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".
Constructors are called during object creation.
No. Logic should never go in a constructor; constructors should only be used to instantiate and initialize object data.
The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.
Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.
You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class. While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior. For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage. For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".
I suppose you want to ask about variable initialization.Java initialize its variables in its constructor.
Constructor is used to do something (written in constructor) immediately after object creation.
initialize simple types: int i = 0; initialize objects: Object o = null; (in java)
Because of the following reasons:static - If a constructor is static, an object instance cannot invoke it to initialize itself (Because static members are not linked to an object)abstract - because an abstract class cannot be instantiated and hence it will not have a constructor. If you make a concrete class's constructor abstract - it cannot be instantiated. eitherways it makes no sensefinal - a constructor cannot be final (thats the way java is designed)
Constructors are called during object creation.
this in java is a keyword that refers to the current object of the class. It is also used in constructor overloading when you want to invoke one constructor from another within the same class.
No. Java does not support copy constructor
A constructor is automatically invoked whenever an object is created. The relevance of this is that the programmer of a class can ensure that the object is initialized properly - for example, that certain fields have sensible values - without having to rely on another programmer, who uses the class, who may, or may not, initialize it in the expected way. A constructor may also be used to simplify object creation. For example, say you have a class, Complex, with two data items (fields) called "real" and "imaginary". Without a constructor, to create an object and assign it a value, you would have to write something like: x = new Complex(); x.setReal(5.0); x.setImaginary(2.0); Or perhaps: x = new Complex(); x.setValues(5.0, 2.0); But with a constructor, this can be combined into a single step: x - new Complex(5.0, 2.0);