0
class s { System.out.println("hello"); }
why this error come
30 ответов
+ 20
Shibbu Patel Every class must have main method to run. So you need to write code inside
public static void main(String [] args) {
}
+ 7
class s
{
public static void main( )
{
System.out.println("Hello");
}
}
u missed the main function in the code
+ 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.
+ 4
Shibbu Patel You can't print directly inside the class without any method or constructor. That's why this error is coming.
+ 3
Shibbu Patel Which error is coming? Please mention with question.
+ 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();
}
}
+ 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) {
}
+ 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}.
+ 2
There is no main method in the code
+ 1
why error come in during compiling the program
+ 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
+ 1
i am not run the program. my question os why error come during the complile
+ 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");}
}
+ 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
+ 1
Aj thanks
+ 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!!!!😅😅
+ 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");
}
}
+ 1
in java class name should always be a capital letter and there must be main method to run a program
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.
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