class pig {
public static void main(String[] args) {
System.out.println("xixi");
System.out.println("haha");
}
private static void main(String name)
{
System.out.println("你");
}
-
Why is my private not executed? – 石生鑫 Mar 18 '23 at 09:19
-
This site is in English, please only comment in that language. – Luuk Mar 18 '23 at 09:22
-
It is not executed because there is a syntax error in it. `System.out.printin` should be `System.out.println` (an `l`, not an `i`) (see: [this](https://stackoverflow.com/questions/9298980/variable-out-of-type-printstream-error-occured)) – Luuk Mar 18 '23 at 09:28
-
No, after I modified it, my private was not printed out in the console – 石生鑫 Mar 18 '23 at 09:29
-
Try adding some proper formatting to your question. Very hard to read that code. – super Mar 18 '23 at 09:36
-
[What is the ``` (3 x backquote) markdown used for?](https://meta.stackexchange.com/questions/189920/what-is-the-3-x-backquote-markdown-used-for) – Luuk Mar 18 '23 at 09:37
-
I did do the edit again, and change the `i` to an `l`. – Luuk Mar 18 '23 at 09:40
-
3@石生鑫 Why should the private `main()` be executed? There is no line in your code that calls your method. – Progman Mar 18 '23 at 09:44
-
This article may be helpful: https://www.geeksforgeeks.org/replacing-public-with-private-in-main-in-java/ – Parzh from Ukraine Mar 18 '23 at 09:46
1 Answers
Java expects a single Main method:
public static void main(String[] args)
you can declare this method to be private and compile, but at run time there won't be a public main method to be found, so your program won't execute. Your second method is with a String not a String[] <- String array
If you just want to execute what's in your second method, call it in the first main method:
class pig {
public static void main(String[] args) {
System.out.println("xixi");
System.out.println("haha");
main();
}
private static void main()
{
System.out.println("你");
}
}
You can't have two methods with the same signature in a single class, this goes for private and public alike, so having a main method as public and then also defining the same signature as private is impossible.
In other words, public int hello()
and private int hello()
are counted as the same signature. On the other hand, public int hello(int x)
and public int hello()
are different signatures and allowed. I hope this clarifies some stuff for you.
The difference between public and private is the following: a private method can only be called within the same class, you can use this to hide methods that are not needed or dangerous to be used from the outside. A public method can be called from the outside.
Let's say you have a class:
class Car{
public hello(){
System.out.println("Hi");
}
private hello2(){
System.out.println("Hello2");
}
}
And a second class
class test{
public static void main(String[] args){
//we create a car instance here
Car c = new Car();
//now we can call the public method here
c.hello();
//but if we try to call a private method we get an error
c.hello2(); //<- this will not execute, is hidden to the outside
}
}

- 100,966
- 191
- 140
- 197

- 34
- 2
-
-
-
Important to be aware of _won't_ and _wont_ have different meanings, same goes for _can't_ and _cant_. The correct use of apostrophes is important for readability. In a similar vein, _thats_ is invalid, it should be _that's_ or _that is_. – Mark Rotteveel Mar 18 '23 at 11:46
-