Table of Contents
- 1 What will happen if I write static public void instead of public static void?
- 2 What if we remove public from public static void main?
- 3 What if a class is private?
- 4 Does it affect if we change the order of public static void main?
- 5 What if static is removed from main method?
- 6 Why TestNG has no main method?
- 7 What is the difference between public void and static void in Java?
- 8 What is the use of public static void main (String args[]) format?
- 9 Why do we write “static” before the main method in Java?
What will happen if I write static public void instead of public static void?
If you write static public void instead of public static void then it is perfectly OK. Your Java program will compile and run successfully. It doesn’t really make any difference as long as method name comes last and return type of method comes second last.
What if we remove public from public static void main?
If we remove static then it becomes instance method,to access an instance method we should create and object and then invoke the method using the object reference. void : Void is the return type of main method.
Can a Java program run without public static void main?
Yes You can compile and execute without main method By using static block.
What if a class is private?
Private classes are allowed, but only as inner or nested classes. If you have a private inner or nested class, then access is restricted to the scope of that outer class. If you have a private class on its own as a top-level class, then you can’t get access to it from anywhere.
Does it affect if we change the order of public static void main?
Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn’t throw any compile-time or runtime error.
Is it mandatory to declare the main () public static and void what will happen if I miss any of the three keywords?
The main() method in Java must be declared public, static and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.
What if static is removed from main method?
If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present. Let’s see what happens when we remove static from java main method.
Why TestNG has no main method?
Because the main() method is needed to run the Java program and while writing tests in TestNg we don’t use main() method, and we use Annotations instead. Annotations in TestNG are lines of code that can control how the method below them will be executed.
Can we declare a private class in Java?
Yes, we can declare a class as private but these classes can be only inner or nested classes. We can’t a top-level class as private because it would be completely useless as nothing would have access to it.
What is the difference between public void and static void in Java?
1 Public: It is an Access modifier, which specifies from where and who can access the method. 2 Static: It is a keyword which is when associated with a method, makes it a class related method. 3 Void: It is a keyword and used to specify that a method doesn’t return anything. 4 main: It is the name of Java main method.
What is the use of public static void main (String args[]) format?
The public static void main (String args []) format is just the default solution people working on the JVM found to call your Java programs, so that there is a definite way to do it.
Can we omit the void while declaring a method in Java?
‘void’ can’t be omitted while declaring methonds as it defines the type of data you want a method to return , it can be only replaced by a valid data type (say int) . ‘static’ is optional , there is no compulsion to use it , if you omit it , you will need to create objects for methods to access them .
Why do we write “static” before the main method in Java?
Hence making it a convention to make the entry method ‘main ()’ as ‘public static void main (String [] args)’ is convenient. What if we don’t write “static” before the main method: If we do not write “static” before the main method then, our program will be compiled without any compilation error (s).