What is the difference between
public static void main(String[] args)
and
public void display
?
Answers
public static void main is a function which the Java program searches for the code to be executed .. whenever a Java program executes it finds main method for the declaration and execution of code and on the other hand public void display is a method which can be called anywhere and can be called inside a main method as well.. moreover the variables declared inside the display method will never go on the program stack until main method calls it
Classes are basically entire objects, that are made up of other objects/attributes. For example, a car could be a class, and it’s public void would be ‘honk’. It could have a color variable that is unique from other cars
What is the difference between public class and public static void main (string[]args)?
Classes are basically entire objects, that are made up of other objects/attributes. For example, a car could be a class, and it’s public void would be ‘honk’. It could have a color variable that is unique from other cars:
public class Car {
public Color color; // this makes a color variable for the car
public void honk() {
// pretend there's code here that makes the car honk
}
}
Also, understanding the difference of keywords; public, static, etc. A public class or method means that it can be run from other classes. This is useful for defining a method you don’t want your program to be able to use, and only other methods inside the same class can use.
Now that we understand the public keyword, we can discuss the static keyword. In Java, you must make an instance of a class before you can access it’s methods
Thus, a static method means you can access the class’s method without even instantiating it. Let’s say our public method “honk” was edited to fit the static keyword
OR
What is the difference between public class and public static void main (string[]args)?
Classes are basically entire objects, that are made up of other objects/attributes. For example, a car could be a class, and it’s public void would be ‘honk’. It could have a color variable that is unique from other cars:
public class Car {
public Color color; // this makes a color variable for the car
public void honk() {
// pretend there's code here that makes the car honk
}
}
Also, understanding the difference of keywords; public, static, etc. A public class or method means that it can be run from other classes. This is useful for defining a method you don’t want your program to be able to use, and only other methods inside the same class can use.
Now that we understand the public keyword, we can discuss the static keyword. In Java, you must make an instance of a class before you can access it’s methods:
public static void main(String[] args) {
Car myCar = new Car();
myCar.honk();
}
Thus, a static method means you can access the class’s method without even instantiating it. Let’s say our public method “honk” was edited to fit the static keyword:
public static void honk() {
// pretend there's code here that makes the car honk
}
Now it is possible to do the following from your main method:
public static void main(String[] args) {
Car.honk();
}
Just so you know, you can’t add a static keyword to a class’s variable.
For more detailed information visit this article by Oracle:
EDIT: After about a thousand views, I decided I’d like to improve this answer. I hope this makes it a ton clearer than before.
5k Views ·
Your feedback is private.
Is this answer still relevant and up to date?
If I rephrase your question it might be "What is the difference between public void abc() and public static void main(String[] args) or public static void abc() and public static void main(String[] args)". Since public class will define a class which is public, it is not comparable to public static void main(String[] args).
Coming back to the question. The two methods are what are known as instance methods and class methods respectively.
Instance methods are called through the reference of an object and they have multiple copies over the number of instances created.
Class methods are methods with the static keyword and are referenced through their class names and not by their objects name. They have a single copy which is shared among each instance.
public static void main(String[] args) is the entry point for any java program and is NOT an ordinary method. It is mainly used to indicate the jvm of the entry point to the program and the program is executed from this point.
Also it is static so that it can be directly referenced by the jvm while the execution of the program.
Hope this will help you ☺
class hello
{ public oid display
{
System.out.print("hello");
}
}
I’d installed the bluejay on my iMac and run the same program but it is only working when I give
public static void main(String[] args)
Wanted to know why