Firstly:
public test(args) {
System.out.println(args);
}
You need a type to go with a parameter - Java is a strongly typed language and thus you always need to specify a type. As to what the type is here, System.out.println() can actually take anything, so you could set the type to String, Object or whatever you like (since Object has a toString() method and it has lots of overloads to deal with all the primitives.) Bear in mind this is unusual though, most of the methods you come across will just take something of a specific type!
Since you're only calling test from the main method here, and you're passing a string to it, you may as well set the type of args to String.
The second problem with this is that there's no return type specified. You always need to specify a return type, in this case nothing is returned so the type is void. If you don't do this then the compiler has no way of knowing whether what you wrote was meant to be a method or a constructor.
The third problem is that test is an instance method but you're calling it statically. test() needs to be static as well, otherwise it belongs to instances of Main and not the Main class. Why does this matter? Well, there could potentially be thousands of instances of Main, so what instance should the method run on? The compiler has no way of knowing.
Next:
public static void main(String[] args) {
test('Hello World');
}
You're passing a string here, which needs to be in double quotes. Java treats quotes differently to PHP, single quotes are used for single character literals and double quotes are used for strings. So you can never enclose a string in single quotes like this, it has to be double.
Putting it all together:
public class Main {
public static void test(String args) { //Add return type and parameter type, make test static
System.out.println(args);
}
public static void main(String[] args) {
test("Hello World"); //Change single quotes to double quotes
}
}
Answer from Michael Berry on Stack Overflowmethods - java beginner "Hello World" - Stack Overflow
java - what is wrong with this code public class Hello { public static void main() { System.out.println("Doesn't execute"); - Stack Overflow
Describe hello world program in java
The death of the public static void main(String[] args) meme
Firstly:
public test(args) {
System.out.println(args);
}
You need a type to go with a parameter - Java is a strongly typed language and thus you always need to specify a type. As to what the type is here, System.out.println() can actually take anything, so you could set the type to String, Object or whatever you like (since Object has a toString() method and it has lots of overloads to deal with all the primitives.) Bear in mind this is unusual though, most of the methods you come across will just take something of a specific type!
Since you're only calling test from the main method here, and you're passing a string to it, you may as well set the type of args to String.
The second problem with this is that there's no return type specified. You always need to specify a return type, in this case nothing is returned so the type is void. If you don't do this then the compiler has no way of knowing whether what you wrote was meant to be a method or a constructor.
The third problem is that test is an instance method but you're calling it statically. test() needs to be static as well, otherwise it belongs to instances of Main and not the Main class. Why does this matter? Well, there could potentially be thousands of instances of Main, so what instance should the method run on? The compiler has no way of knowing.
Next:
public static void main(String[] args) {
test('Hello World');
}
You're passing a string here, which needs to be in double quotes. Java treats quotes differently to PHP, single quotes are used for single character literals and double quotes are used for strings. So you can never enclose a string in single quotes like this, it has to be double.
Putting it all together:
public class Main {
public static void test(String args) { //Add return type and parameter type, make test static
System.out.println(args);
}
public static void main(String[] args) {
test("Hello World"); //Change single quotes to double quotes
}
}
In Java, Strings are always using double-quotes, never single-quotes. That's reserved for char types.
test("Hello World");
Also, your test function doesn't have a return type (not even void), and it's a method while main is static, so you'd need to instantiate Main or make test static as well. You also need to specify the type for each argument.
Try this:
public static void test(String args) {
System.out.println(args);
}
It should be:
public static void main(String[] args)
This is what your code should look like:
public class Hello {
public static void main(String[] args) {
System.out.println("Doesn't execute");
}
}
Notice the closing parenthesis, also I have properly changed your main method.
Here's another hint:
When you create a new Java class in Eclipse, there is an option to auto-generate the main method stub for you (this option would have fixed your error without you even knowing).
It is the first checked checkbox in the following screenshot.

So I'm a newbie developer (1 yr exp), trying to get an understanding of core java. I've been coding for my company for a year now, but writing business logic doesn't seem to be helping much when it comes to improving my understanding of core java. Thought of doing an exercise of taking the simplest programs and going into detail about everything written in it.
Here's a simple hello world program in java:
class HelloWorld
{
public static void main(String []args)
{
System.out.println("Hello world");
}
};Can I get as many details about what's happening in this file as possible? Like really go into the details here. Explain what each keyword does, why the print statement doesn't require an import, etc.