answersLogoWhite

0

The constructor of a class is automatically called when an instance of the class is created (using new in C++). The constructor method has the same name as the class that it is a part of. Constructors have no type and do not return anything.

Similarly, the destructor is automatically called when the instance of the class is destroyed. The destructor is the same name as the class and is preceded by a tilde (~)

For example:

class Example

{

public:

Example() // Constructor

{

printf("Object created\n");

}

~Example() // Destructor

{

printf("Object destroyed\n")

} };

int main()

{

Example* x = new Example(); // Creates object, calls constructor

delete x; // Calls destructor, deletes object

return 0;

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Continue Learning about Engineering

.....will be automatically Invoked when an object is created?

The Class object is automatically created by the JVM when an object is created. The Class object provides information about the Class and is primarily used by the IDEs and factory classes. The method that is automatically called when an object is created is called a constructor. In Java, the constructor is a method that has the same name as the class.


What method can be called without the instance of a class?

static method


What is the difference between a class method and an instance method?

Instance methods can be called by the object of a Class whereas static method are called by the Class. When objects of a Class are created, they have their own copy of instance methods and variables, stored in different memory locations. Static Methods and variables are shared among all the objects of the Class, stored in one fixed location in memory.Static methods cannotaccess instance variables or instance methods directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


Is object of superclass created when object of derived class is created?

Not as a separate instance. The derived class instance can be used anywhere a superclass instance is expected, so it is an instance of the superclass in that respect, and a superclass constructor will be called at creation time. However, it will always act like an instance of the derived class, even if it is explicitly cast to the superclass. So overridden methods will always call the derived class method, regardless of how an outside caller refers to the instance.


Can you override an instance method and make it final?

No. Once a method is declared final in a class, no derivative of that class can override that method.

Related Questions

.....will be automatically Invoked when an object is created?

The Class object is automatically created by the JVM when an object is created. The Class object provides information about the Class and is primarily used by the IDEs and factory classes. The method that is automatically called when an object is created is called a constructor. In Java, the constructor is a method that has the same name as the class.


What are destructor in java?

Destructors in Java are called finalizers. Every class can define a finalize() method that will get called automatically by the garbage-collector when an instance of the class gets garbage-collected. Finalizers are not guaranteed to get called, as the instance might never get collected.


What method can be called without the instance of a class?

static method


How do you call a method from inside the main method?

I assume the question is about Java. How you call a method from inside the main method depends on whether this is an instance method or a static method. To call an instance method, you need to have a valid instance. Say foo is an instance of class Foo, which was created inside the main method. Then to call the instance method implode() use foo.implode() To call the static method bar() of class Foo, use Foo.bar().


What is the difference between a class method and an instance method?

Instance methods can be called by the object of a Class whereas static method are called by the Class. When objects of a Class are created, they have their own copy of instance methods and variables, stored in different memory locations. Static Methods and variables are shared among all the objects of the Class, stored in one fixed location in memory.Static methods cannotaccess instance variables or instance methods directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


Is object of superclass created when object of derived class is created?

Not as a separate instance. The derived class instance can be used anywhere a superclass instance is expected, so it is an instance of the superclass in that respect, and a superclass constructor will be called at creation time. However, it will always act like an instance of the derived class, even if it is explicitly cast to the superclass. So overridden methods will always call the derived class method, regardless of how an outside caller refers to the instance.


What is the diffferent between method and constructor?

Both are functions, i.e., places where you can write code. A constructor is simply a special method that is invoked automatically when an object is created.


Can you override an instance method and make it final?

No. Once a method is declared final in a class, no derivative of that class can override that method.


What is instance?

An individual object of a certain class. While a class is just the type definition, an actual usage of a class is called "instance". Each instance of a class can have different values for its instance variablesA case or occurance of something.


What is instance method?

An instance method is nothing but a normal java method. They can be invoked only by creating an object of the class that contains the method definition. Unlike static methods that can be invoked without creating an object of the class.


What is parent class in java?

Object Class is the parent class of all classes in java.Every class in the Java system is a descendant (direct or indirect) of the Object class.


What is the valid method for disabling a session?

In order to dispose of a session that is no longer needed, you can call the invalidate() method of the HttpServlet class. Assuming you have an instance of HttpServlet called 'session' you would do this: session.invalidate();