answersLogoWhite

0

The constructor will be automatically run when you create the object.

public class Hello {

public static void main(String args[]) {

Hello objectName = new Hello();

}

public Hello() { //this is the constructor, it must have the same name as the class

System.out.println("Hello");

}

}

When you run this, the main will only create the object, and "Hello" will be printed on screen, notice that you didn't have to use objectName.Hello(); or anything like that.

Once the object has been created you won't be able to use the constructor again, but you can copy/paste it into another method and use that.

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

What is the uses of constructor in java?

The constructor will invoke all constructors in the inheritance hierarchy to ensure that all the parent classes of the current classes get initialized when the current class is instantiated.


What is the use of constructors?

Constructors are used in object-oriented programming languages to create usable instances of abstract data types (classes).


What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors


Can you call a constructor from another if a class has multiple constructors?

Yes. All you need to do is to specify the correct number of arguments to invoke the correct constructor.


Why are constructors dynamic rather than static by default?

Because the JVM decides are run time which constructor to invoke depending on the parameters passed to it


How can you justify the use of constructor and destructor in c plus plus?

Constructors allow class designers to initialise class attributes at the point of instantiation (whenever an object of the class is created). All classes must have at least one constructor other than the copy and move constructors otherwise it would be impossible to instantiate objects of the class. Copy and move constructors are not required but are generated automatically by the compiler unless the class designer explicitly marks them deleted from the class definition. The copy and move constructors allow new instances to be constructed from existing instances. The only difference between the two is that the move constructor transfers ownership of the member attributes, rather than merely copying them. In classes that contain pointers, the compiler-generated copy constructor only copies the pointer (a shallow copy) not what it points at (a deep copy). Thus class designers must provide a copy constructor in order to deep copy any unshared memory resources. Derived class constructors automatically invoke base class constructors, so classes are always constructed from the bottom up. Copy and move constructors automatically invoke their base class copy and move constructors, respectively, however all other constructors automatically invoke their base class default constructors, which may not be the most efficient method of construction. Thus class designers can invoke specific base class constructors through the constructor's own initialisation list (which is also used to initialise member attributes). Constructor bodies do not require any code except in those cases where initialisation is not possible with the initialisation list alone, however this is usually an indication of poor design choices rather than a limitation of the initialisation list. The initialisation list provides the most efficient mechanism for class initialisation. Every class must have one (and only one) destructor. If one is not provided by the class designer, the compiler generates one for you. However, unless the class contains a pointer to unshared memory, there is no need to define your own. Otherwise you must provide a destructor in order to release the memory. The destructor is the last chance to do so before an object of the class falls from scope. Classes that are intended to act as base classes must provide a virtual destructor (that is, the lowest base class must declared the destructor virtual). This ensures that if an object is destroyed polymorphically (via a pointer to one of its base classes), the most-derived destructor is always invoked first. Destruction of hierarchies is always top down. Note that destructors are not virtual by default for the simple reason that not all classes are intended to act as base classes. The containers provided by the Standard Template Library (STL) are a good example of this as none of the containers have virtual destructors -- so they cannot be used as base classes. Note that if any class method is declared virtual, the destructor must also be declared virtual. However, when inheriting from a base class with a virtual destructor, the class destructor is implicitly virtual (as are all virtual functions inherited from base classes) and does not need to be specified, although its good practice to explicitly include it anyway.


Why constructors are declared public only?

Constructors are not declared public only. They can be declared protected and private, as required by the class. Protected constructors are only accessible to the class members and to derived classes. Private constructors are only accessible to the class members. Although a default public constructor is required by the majority of classes, it is not true of all classes. Derived classes can call any public or protected base class constructor explicitly via their own construction initialisation sections. Private construction is typically used in the singleton model to instantiate a private static instance of the class as and when it is required, whilst preventing multiple instances of the class from being created. However, class members also include friends of the class, thus external friend classes and friend functions can also instantiate objects via their private constructors. Note that the whole point of public, protected and private access is not to hide information (as is often wrongly said) but in order to limit access to that information. The same applies to a class' constructors as it does to its member methods and member variables.


What is dynamic constructor?

In Java classes we can declare multiple constructors. The JVM would dynamically decide which constructor to invoke based on the parameters passed from the calling class. Ex: public class Test { public Test(){ ... } public Test(String arg1){ ... } public Test(String arg1, int arg2){ ... } } In the above class, there are 3 different constructor declarations. Whenever a constructor is invoked from a calling class, the JVM would decide which one to invoke based on the number of arguments passed.


Can you declare constructor as constant if yes why?

Constructors are implicitly constant members of a class. They do not modify objects, rather they initialize them. So constant and not constant objects can invoke them: const MyClass object; // invokes MyClass::MyClass() constructor


What are the various concepts of c plus plus?

1.Classes and Objects 2.Constructors and Destructors 3.Inheritance 4.Polymorphism 5.Dynamic Binding


What is prefix of invoke?

The prefix of "invoke" is "in-".


What is mean by Classes and Objects?

an Object is an instance of the Class for ex: Integer i = new Integer (1); i is an object of the Class Integer. of courses classes contain variables and methods and constructors. although the Object Class is a different case, it's the main class that all the classes in java are subClasses of it