answersLogoWhite

0

Is it possible to create a static object in main?

Updated: 8/17/2019
User Avatar

Wiki User

14y ago

Best Answer

Yes, anywhere that you can normally declare an object you can use the static keyword to make that object static.

int main ()
{
static Object o;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it possible to create a static object in main?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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 the use of static in public static void main?

The static keyword signifies that the method belongs to the class and not any of its objects. Hence, the JVM does not have to create an object of that class before beginning execution. Imagine how the JVM would create an object and invokes its method even before the class execution starts. That is why the main method is static and is the starting point of any java application.


Is it possible to call static methods with out creating object?

Yes, we can access all methods which declare with static means then we can access.. ex: class s{ static method() { System.out.println("Welcome"); } } class fun{ public static void main(String args[]) { method(); } }


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.


Why main is static?

The main method is static because the JVM would be invoking this method as the starting point of execution. If this is like other normal methods, the JVM would have to instantiate an object of the class before it can call it. This is not possible because this is the starting point. If the main method is static the JVM can directly call this by specifying the class name.


Is it possible to define a java static method inside a main method?

Because, the main method is the starting point of the java program and if we need an object of that class even before the main can be invoked, it is not possible. Hence it is declared static so that the JVM Can acess the main method without having to instantiate that particular class


Assignment object oriented data and process simple task?

write a class implementation for a class_my class that contains the data member ID of type integer and data member MSG of type string (to hold a simple description of any created object (local,global,static,automatic))then follow what come serially 1-create a global void function named create_fun to create 2 objects:obj1 local automatic to create_fun,and obj2 static to create_fun. 2-constructor with 2 arguments to initialize object data member ID and MSG with any given value. 3-a destructor to print the ID and MSG of any terminated object. 4-a global object named obj3. 5-a local object to the main function named obj4. 6-a static local object to the main function named obj5. 7-call function create_fun from the main function. 8-create a local object named obj6 to the main function. 9-call function create_fun from the main function. 10-create a local object named obj7 to the main function.


Which method is called first of all other methods when any application executed in c sharp?

static method Main() of the start object/class.There are 4 different signatures of Main():static void Main();static int Man();static void Main(string[] args);static int Main(string[] args);The starting object/class can have only one of them. Other classes or objects may have Main() method as well. (But why would be the question, Main() is NOT a good OO method name anyway. Method name should be a verb or start with a verb)


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 public static void main?

The main method is the starting point of any Java Programs execution. It is public - so that anyone can access it static - so that the JVM can call it without the necessity to create an object void - because the method doesnt actually return anything. It just kick-starts the program execution


Why main method is static?

Main method in java is always static as main method id the only method from where the program execution starts,and as we all know that main method is defined inside a class so JVM needs to make a object of the class to call the method and objects are build inside the main method ,so to execute the main method it has to make objects of the class but objects are build inside main method so that's the reason that main method is static so that JVM can execute the main method without making its object as static members can be called by class anme only


Why you use main method when you can use static method?

The main method is called by the jvm when your program is executed. The main method is a static method: public static void main(String[] args) {} A static method is method that can be run without instantiate the class (creating an object from it) The main method is a static method. No other static method could replace it's functionality. PS By static method do you mean static initialiser? I often use static initialisers instead of a main method, but in these cases you must still have a main method, albeit an empty one ie. public static void main(String[] args) {} Notice that the method has an empty body A main method must be used if you intend to accept parameters at run time from the jvm.