answersLogoWhite

0


Best Answer

please clarify the question. I have a feeling that the question is not asking what I am about to answer here.

You may have a class named "main" in the program.

I presume non-main means any class that is not main.

Can an object be created from a class (named main or non-main)? It depends on how you design that class. Ususally yes.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can an object be created from a main or non-main class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A method that is automatically called when an instance of a class is created?

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; }


How do you create an object type object in c plus plus?

An object is simply an instance of a class. #include<iostream> class my_object {}; int main() { my_object X; // instantiate an instance of the class my_object, identified as X. }


Can we create an array of objects for a class having default constructor Justify your answer?

See example code below. #include <iostream> class x { private: // Members. static int counter; int data; public: // Default constructor and destructor. x():data(++counter){printf("Object %d created!\n",data);} ~x(){printf("Object %d destroyed!\n",data);} // other members omitted for brevity... }; // Initialise static counter. int x::counter=0; int main() { // Instantiate an array of 10 objects. x arr[10]; // .. do some work with the array .. return( 0 ); // The array falls from scope, destroying the objects. } Example output: Object 1 created! Object 2 created! Object 3 created! Object 4 created! Object 5 created! Object 6 created! Object 7 created! Object 8 created! Object 9 created! Object 10 created! Object 10 destroyed! Object 9 destroyed! Object 8 destroyed! Object 7 destroyed! Object 6 destroyed! Object 5 destroyed! Object 4 destroyed! Object 3 destroyed! Object 2 destroyed! Object 1 destroyed!


In java what is the difference between an object and a class?

An object is an _instance_ of a class. A good example is to think of a class as a blueprint for a house, whereas an object is the actual house. You can have many, many houses (objects) build from the same blueprint (class)


What is the purpose of constructor in object oriented programming?

A constructor in a class is one of the first pieces of code to be executed when you instantiate a class. The purpose of constructors is to have code that initialize the class and prepare the variables that may be required by the class...

Related questions

A method that is automatically called when an instance of a class is created?

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; }


How do you create an object type object in c plus plus?

An object is simply an instance of a class. #include<iostream> class my_object {}; int main() { my_object X; // instantiate an instance of the class my_object, identified as X. }


Why static is used in main method?

It is declared static because the JVM would need to execute the main method and if it is not static the JVM would need an object of the class to access the method. How can the JVM get an object of a class without invoking it. since it is static, the JVM can easily access it without this need to have an object of the class.


Can we create an array of objects for a class having default constructor Justify your answer?

See example code below. #include <iostream> class x { private: // Members. static int counter; int data; public: // Default constructor and destructor. x():data(++counter){printf("Object %d created!\n",data);} ~x(){printf("Object %d destroyed!\n",data);} // other members omitted for brevity... }; // Initialise static counter. int x::counter=0; int main() { // Instantiate an array of 10 objects. x arr[10]; // .. do some work with the array .. return( 0 ); // The array falls from scope, destroying the objects. } Example output: Object 1 created! Object 2 created! Object 3 created! Object 4 created! Object 5 created! Object 6 created! Object 7 created! Object 8 created! Object 9 created! Object 10 created! Object 10 destroyed! Object 9 destroyed! Object 8 destroyed! Object 7 destroyed! Object 6 destroyed! Object 5 destroyed! Object 4 destroyed! Object 3 destroyed! Object 2 destroyed! Object 1 destroyed!


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


In java what is the difference between an object and a class?

An object is an _instance_ of a class. A good example is to think of a class as a blueprint for a house, whereas an object is the actual house. You can have many, many houses (objects) build from the same blueprint (class)


What is the purpose of constructor in object oriented programming?

A constructor in a class is one of the first pieces of code to be executed when you instantiate a class. The purpose of constructors is to have code that initialize the class and prepare the variables that may be required by the class...


How it is created classes?

class is a group of objects that share common properties and relationships. -: class is a keyword. -:class is a blue print of objects. -:class is a declaration , template or a blueprint of objects that is classifying by a object. creating a class with java code-: public class sonu { public static void main (string [] args) { system.out.println(" abhishek is the student of niit bihar sharif nalanda"); } }


Why main function written as public static in java?

They are public because they must be accessible to the JVM to begin execution of the program. Main is the first method that would get executed in any class.They are static because they must be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static.OrMain is starting point of the program execution Main method is called by JVM there is no need for JVM tocreate an object of the class just to access Main function


What is difference between classes and objects?

Posted by : Govind Singh Lodhithese are the following differences between java class and object.1. Class is a way of bind data member and method in single unit where as a object is a value form of a class or a instance of a class that means allocating sufficient amount of memory space.2. When we define a class ,there is no memory space allocated for data member of a class where as when we create a object of a class memory space is allocated for data members of a class.3.Class will have exist logically where as object will have exist physically.4.One class can have only one name where as corresponding one class we can create multiple object.5.Class load in main memory by using class loader subsystem where as after load class we can create multiple objects of a class.


Can main method be non-static?

No, the reason is simple: A method marked as a static means, that you don't need create an instance (create an object with the 'new' operator) in order to use it. The main method is the entry point for a java application, therefor there is nothing after you call it. no one who can create an object of the type of your class and call the main method. So the jvm must call the main method with no object reference.


What is an objection?

An object is an instance of a class. To understand what is meant by an instance here is a small example. if we have a class Person as class Person{ public String name; public int age; public float salary; public Person(String name, int age, float salary){ this.name=name; this.age=age; this.salary= salary; } public static void main(String args[]){ Person personObject = new Person("Abhay",45,50000); } } In the main function we have created an object of type Person. we can now use this object to manipulate the contents of the object such as the name,age and salary of the person. Other methods of the class have not been included.An object, traditionally defined, is either a direct object or an indirect object.An object, in some usages, is any grammatical relation other than subject.