Why main() is declared public and static in java [duplicate] - java

This question already has answers here:
why main method can't be of default scope? [duplicate]
(9 answers)
Why is the Java main method static?
(37 answers)
Closed 8 years ago.
Why is the main declared as public and static?
public static void main(String arg[])
{}
acording to ans in java
"The method is static because otherwise there would be ambiguity: which constructor should be called?"

public - The main method is called by the JVM to run the method which is outside the scope of the project therefore the access specifier has to be public to permit a call from anywhere outside the application.
static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.
void - Java is a platform independent language, therefore if it returns some value, then the value may have a different meaning between different platforms so unlike C it can not assume a behavior of returning value to the operating system.

Related

Difference of args vs arg in main-method, java? [duplicate]

This question already has answers here:
Difference between String[] arg and String[] args?
(7 answers)
Closed 2 years ago.
Why does
" public static void main(String[] arg) { "
work instead of
"public static void main(String[] args) { " ?
Does it have anything to do with the String-Array?
And how exactly does it work?
May using "arg" instead of "args" just be a tiny bit more efficient?
There is absolutely no difference, beyond the normal differences of parameter names (as in, you have to use the right name within the method body). The name of the parameter to the main method is entirely by convention. It's entirely valid to write:
public static void main(String[] dontUseJavaUtilDate)
as the declaration for your entry point.
From JLS 12.1.4 which uses Test
Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.
The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.
There's no specification of the parameter name.
Ditto from the JVM specification section 5.2:
The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

Why is the following code working with an interface but without any class defined? [duplicate]

This question already has answers here:
Why are interfaces in Java 8 allowed to have the main method?
(5 answers)
Closed 5 years ago.
interface Main
{
public static void main(String[] args)
{
System.out.println("Inside main");
int a = 4 , b = 6 ;
System.out.println(a+b);
}
}
In the above code, there is no class defined but the program is still getting executed. But as far as I know, there cannot be any static method inside an Interface. And, every program should contain at least one main function.
Because, you are using Java version 8.
From Java 8 on, you are allowed to have static methods inside an interface.
And main() gets run from interfaces as well (even from enums), as long as you keep the correct signature.

Why instance method cannot override the static method [duplicate]

This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 5 years ago.
class Abc{
public static void hello(){
System.out.println("parent");//Line1
}
}
class Abc1 extends Abc{
public void hello(){//Line2
System.out.println("child");//Line3
}
}
Compiler gives error at line 3 saying that
This instance method cannot override the static method from Abc
Why can static methods not be overridden by a instance method?
Simple: because the language specification says so.
That is one of the downsides of static methods: there is no polymorphism for them! Conceptually, they are not meant to be overridden. That is all there is to this.
To be precise: the JLS says differentiates between static and non-static method and states:
An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.

Non-static method cannot be referenced from static context [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
New to Java, trying to figure out how to resolve this issue.
boolean myBool = G(A,n,m,0);
For some reason it isn't like this line. Why won't it let me call this simple function? Both main() and G() are part of class C().
A non static method belongs to a specific instance of a class, while a static method belongs to the class itself. Inside main, which is a static method, you cannot reference non-static methods without having a specific object to run them. E.g.:
boolean myBool = new C().G(A,n,m,0);
However, if the class has no interesting state, or it's state does not effect the method G, you should define G as static.
It's likely because you didn't include static in the definition of the G() method.
Main() is a static method, and since static things run before non static things do, static things can only call/use static things.
Note that your Main() doesn't require you to make a C object. It's the entry point to the program, and it doesn't make sense if you have to first make an object in order to run your program - where would you make that object from?
If you want to make non-static calls, create objects of the corresponding class.

Why are static variables and methods in java called directly? [duplicate]

This question already has answers here:
What is the actual memory place for static variables?
(7 answers)
Closed 9 years ago.
Why static variables are directly called without any use of object in java?Are they stored in some different memory location?And why only static methods can called with the name of the class directly without creating its object ?for example
class First
{
public static void ss()
{
System.out.println("This genius will give answer");
}
}
class Second
{
public static void main(String ar[])
{
First.ss();
}
}
Yes static resources belong to the class and not the objects. And are stored in a separate location kind of global location. You can read more here.
As docs says
Every instance of the class shares a class variable, which is in one fixed location in memory.
The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in
ClassName.methodName(args)

Categories