0

class s { System.out.println("hello"); }

why this error come

22nd Mar 2020, 7:40 AM
Shibbu Patel
Shibbu Patel - avatar
30 Respostas
+ 20
Shibbu Patel Every class must have main method to run. So you need to write code inside public static void main(String [] args) { }
22nd Mar 2020, 7:55 AM
A͢J
A͢J - avatar
+ 7
class s { public static void main( ) { System.out.println("Hello"); } } u missed the main function in the code
22nd Mar 2020, 7:51 AM
Anuhya Anu
Anuhya Anu - avatar
+ 5
Sunny Akhil To compile program no need to write main method. We can compile without main method also. public class A { A() { System.out.println("@"); } } compile above program it will work fine without main method aslo. His code is not compiling because he is printing without any method. Avinesh He is getting compile time error as you told.
22nd Mar 2020, 12:02 PM
A͢J
A͢J - avatar
+ 4
Shibbu Patel You can't print directly inside the class without any method or constructor. That's why this error is coming.
22nd Mar 2020, 8:26 AM
A͢J
A͢J - avatar
+ 3
Shibbu Patel Which error is coming? Please mention with question.
22nd Mar 2020, 8:20 AM
A͢J
A͢J - avatar
+ 2
Shibbu Patel Yes but how default constructor will know that what you are printing. Default constructor doesn't mean that it can print something. Do like this. public class Program { Program() { System.out.println("@"); } public static void main(String[] args) { new Program(); } }
22nd Mar 2020, 8:30 AM
A͢J
A͢J - avatar
+ 2
Shibbu Patel Default constructor is className() { //if you are printing something or you want to declare something then you should make the default constructor } parameterized constructor is className(int a, int b) { }
22nd Mar 2020, 8:34 AM
A͢J
A͢J - avatar
+ 2
Shibbu Patel For your program to run you need a main method. Since the main() method is considered as the entry point of the program, the JVM will look for it and throws an error if it is not found. This was at runtime. You must be getting an error during compile time because the print statement is not included inside a method. This is because you cannot call a print statement just using the class name. One way to escape error is that you can include the print statement inside the static block for eg- static{print here}.
22nd Mar 2020, 9:20 AM
Avinesh
Avinesh - avatar
+ 2
There is no main method in the code
23rd Mar 2020, 12:21 PM
Saumya Tripathi
Saumya Tripathi - avatar
+ 1
why error come in during compiling the program
22nd Mar 2020, 7:53 AM
Shibbu Patel
Shibbu Patel - avatar
+ 1
We can't run the code without main function No matter how many functions u have inside ur code the main functions runs first. It is the permanent syntax main shld be included in the code
22nd Mar 2020, 8:02 AM
Anuhya Anu
Anuhya Anu - avatar
+ 1
i am not run the program. my question os why error come during the complile
22nd Mar 2020, 8:03 AM
Shibbu Patel
Shibbu Patel - avatar
+ 1
class s { System.out.println("hello"); } this program is not complie.but this program is complie class s { void show() { System.out.println("hello");} }
22nd Mar 2020, 8:07 AM
Shibbu Patel
Shibbu Patel - avatar
+ 1
class s { System.out.println("hello"); } this program is not complie.but this program is complie class s { void show() { System.out.println("hello");} } This code works as there is some function inside the class
22nd Mar 2020, 8:14 AM
Anuhya Anu
Anuhya Anu - avatar
+ 1
Aj thanks
23rd Mar 2020, 3:59 AM
Shibbu Patel
Shibbu Patel - avatar
+ 1
In java the first class which is called when we run the program is the class in which main method is written. In your program the main method class is missing.Also print statement is written inside any of the method which you have written directly inside class without specifying any method. Either add main method to existing class or create new class with main method and call the already existing class method in this main method.🙂 Hope you understood it!!!!😅😅
23rd Mar 2020, 4:58 PM
AKSHAT AGRAWAL
AKSHAT AGRAWAL - avatar
+ 1
AKSHAT AGRAWAL Main method is not his problem. His problem is that why code is not compiling. We can compile class without main method also but we can't print like that he is printing. We should print inside method or constructor or static block. If we do like that then code will compile. So this code will not compile class s { System.out.println("hello"); } and this code will compile class s { static { System.out.println("hello"); } }
23rd Mar 2020, 5:06 PM
A͢J
A͢J - avatar
+ 1
in java class name should always be a capital letter and there must be main method to run a program
25th Mar 2020, 5:48 AM
Kavya
Kavya - avatar
0
class s { System.out.println("hello"); } this program is not complie.but this program is complie class s { void show() { System.out.println("hello");} } This code works as there is some function inside the class. then why error come 6when i remove function.
22nd Mar 2020, 8:20 AM
Shibbu Patel
Shibbu Patel - avatar
0
D:\advance java\inner>javac s.java s.java:3: error: <identifier> expected System.out.println("hello"); ^ s.java:3: error: illegal start of type System.out.println("hello"); ^ 2 errors
22nd Mar 2020, 8:21 AM
Shibbu Patel
Shibbu Patel - avatar