I have written a simple java program mentioned in below. Unfortunately a compile error occurs.
class String {
public static void main(String[] args) {
System.out.println("stre");
}
}
The following comes out during compilation at command prompt:
c:\Java>java String
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args)
It wasn't working for any of my programs, not even this simple one! Why is this?
EDIT:
Now I have:
import java.lang.*;
import java.lang.String.*;
class incogn {
public static void main(String[] args) {
System.out.println("stre");
}
}
And its not working. Why isn't that working?
It says the exact thing as before.
What you said with the Java.lang.String[] works, but why won't this? And why haven't I needed to put any of this on before?
It's probably because you are using the name String for your class, colliding with java.lang.String; in other words, you have written that your main method takes instances of your class as input, rather than the one that the main method needs to take, namely java.lang.String. Try either
Renaming your class
Changing your signature to public static void main(java.lang.String[] args) {
You should not call your class String, it will conflict with the java class String.
Or, you can change your code to:
class String {
public static void main(java.lang.String[] args) {
System.out.println("stre");
}
}
Don't create a java file named "String" as it is the java library class in java.lang.String
Make the class as public
So, write your source code as below:
public class StringDemo {
public static void main(String[] args) {
System.out.println("stre");
}
}
Compile it now. I believe it would work well.
1) The class should be public
2) Avoid to call you class String, as it has the duplicate name with java.lang.String (You still can, but not good). Otherwise, your main method is having wrong type of arguments.
3) You don't have to explicitly import from java.lang package.
public class String {
public static void main(java.lang.String[] args) {
System.out.println("stre");
}
}
OR
public class YourClass {
public static void main(String[] args) {
System.out.println("stre");
}
}
Try to use the second form.
Well, giving your class name String and then call main(String[] args) is ambiguous, if it means java.lang.String or your newly defined class, but with java scope rules, it will use your, unless otherwise declared.
Update after edit
There's no java.lang.String package, but it is a class. Therefore, you can't say
import java.lang.String.*;
By adding the ".*" the compiler understands that this refers to some package String.
One exception, is the static import, but this is not what you need here.
Related
I have recently started learning java and its my first OOP language.I read that static methods do not require the class to instantiated they run when you feed the class to the JVM.My question is what would happen if the static method is inside a private nested class. will it still run?
EDIT- I tried it does not work, I want to know what is happening in the background.
public class tester{
private class estupid{
public static void main(String[] args){
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
To the people down voting, A suggestion,A more productive activity would be telling what's wrong with the snippet, Thank You.
There are many mistakes, which you can simply get by compiling the code. I suggest you to use command line javac compilation
If you compile your code as it is
C:\src>javac tester.java
tester.java:3: error: Illegal static declaration in inner class tester.estupid
public static void main(String[] args) {
^
modifier 'static' is only allowed in constant variable declarations
1 error
As per above error, make your nested class as static nested class. Now the code will compile successfully but you will get error while running it:
C:\src>javac tester.java
C:\src>java tester
Error: Main method not found in class tester, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
With above error you can understand that you are running tester class, but it does not contain any main method which is searched by the JVM. So add a main method in tester class and yes you can call the static method in static inner class. Changed code will be like this which will run properly:
public class tester {
private static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
public static void main(String[] args) {
estupid.main(args);
}
}
After compiling and running the above code
C:\Himanshu\GitHub\hsingh-learning\src>javac tester.java
C:\Himanshu\GitHub\hsingh-learning\src>java tester
Hello Im a static method of a private class and main too
This is just to correct your code and making it compilable and runnable, BUT writing main method in nested class is not suggested.
Other thing is that you are making private nested class, so you are making it unaccessible from outside of the holding class (tester class in your case). tester class is public and accessible to JVM but the nested class is marked private so that cannot be accessed.
That does not mean you cannot invoke main static method of nested class from JVM. Make your nested class public.
public class tester {
public static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
Compile it, which will produce 2 class files.
1. tester.class
2. tester$estupid.class
Run the 2nd tester$estupid which contains the main method (which is required by JVM)
C:\Himanshu\GitHub\hsingh-learning\src>java tester$estupid
Hello Im a static method of a private class and main too
The main method must be a member of a public class. A static method is a method that is a child of the class itself rather than an object, or an "instance" of that class.
Okay so I'm learning java and everything was working fine yesterday, and all of the sudden today all of the stuff i wrote (just some super basic stuff) no longer work, for example
package sectionOne;
public class AddingNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 10;
int y = 20;
int result = x+y;
System.out.println(result);
}
}
gives me error:
Error: Main method not found in class sectionOne.AddingNumbers, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Unless I change this:
public static void main(String[] args) {
to this:
public static void main(java.lang.String[] args) {
the program won't work. Also when I create a string like
String x = "test";
it won't work unless I do
java.lang.String x = "test";
What's going on?
You've got your own class named String that is causing conflicts with the compiler -- rename it to anything else (that doesn't conflict with core class names that is).
You've got a class that has the same name as the java.lang.String class in your namespace so:
1->try to search in every import and, if you find some String class imported, remove it.
2->search in your package if there is a class named String and rename it
if you did this and it still doesn't work, then your compiler has some problems :/
Please help me out in out to get rid of this error. Whereas i tried the same code in Command line it was working perfectly without any exceptions.
public class String {
public static void main(String[] args) {
StringBuilder nb = new StringBuilder("");
nb.append("<string1>");
nb.append("<string2>");
System.out.println(nb.toString());
}
}
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args) or a JavaFX application class
must extend javafx.application.Application
Don't name your class String as String is also a "build-in" type of Java (java.lang.String).
I'm still a bit knew to Java. Only done PHP and a bit of javascript most my life. I had a question concerning the order you have your classes/methods encapsulated inside each other. Does it matter? Is there a better professional way with doing this? Sorry for the newbie question.
Method 1 (class after main())
public class myClass {
public void main(String[] args) {
//Body Code Here
}
}
class myClass {
//Body Code Here
}
Method 2 (class inside main())
class mainClass {
public void main(String[] args) {
//Body Code Here
class myClass {
//Body Code Here
}
}
}
Method 3 ( class before main())
class myClass {
//Body Code Here
}
class mainClass {
public static main(String[] args) {
//Body Code Here
}
}
You need to have all your methods inside the body of your class. Outside it, you can only import some libraries for example java.util.Scanner(for reading from the console).
And you can insert methods before or after the Main method, both ways when you run the program they will be read.
Your main method, as any other method, must be within a class. Therefore only the first method would work.
Your first method will work.
as the main method should be in the class then only jvm will compile it.
I typed this code into Eclipse
public class Hello
{
public static void main()
{
System.out.println("Doesn't execute");
}
// .....
}
When I press run it says that it does not contain a main type.
I don't know what I'm doing wrong , and I am new to java.
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.
main method without the string array arguments is not the method that JVM looks for to start the execution of a class.
After completion of the initialization for a class (during which other consequential loading, linking, and initializing may have occurred), the method main of class is invoked.
The method main must be declared public, static, and void. It must specify a formal parameter whose declared type is array of String. Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
Read more about JVM startup, loading, linking and intilization of class here:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4