Bad english alert
Whenever I try to use System.out.println on another class besides main, every single IDE installed in my PC returns the error on the title.
I'm writing a really simple code.
On IntelliJ, I had already tried to use "Invalidated caches" but didn't work as well.
Works here:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
But not in this other tab:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
UPDATE
Here some images to specify the problem: Work here, but not here.
All code has to be in methods.
The command System.out.println("") will work only in a method.
Placing it under a class but not a method will result in the compiler throwing an error.
Eg:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
will work perfectly fine.
But, if you place the command under just a class, it's going to result in an error.[Needs to be in a certain method]
Eg:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
Also, you need to make sure your class and function are not reserved keywords.
And from what you've specified above, main in not a class but it's the main method of the class.
Related
File Name = multiple_main_methods.java
class multiple_main_methods_two {
public static void main(String[] args){
System.out.println("Class second");
}
}
class multiple_main_methods_one {
public static void main(String[] args){
System.out.println("Class first");
}
}
Output
Class first
IDE used - IntelliJ IDEA
IntelliJ is choosing a class to execute.
Remember, you execute the main method in a class, not in a file.
Check it in your Run configurations
enter image description here
It is not giving error because your main methods belong to different classes i.e. multiple_main_methods_two and multiple_main_methods_one which is completely fine.
I need help with the main method, I'm getting this error: while compiling the Project:
Error: Main method not found in class Main, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Also tried online Java compilers but no luck.
public class Main{
String yourname = "Ayush Sahu";
int mnumber = 25;
public void justtest(){
System.out.println("This is a test Method!!!");
}
}
class testing extends Main{
String fname = "Balamukunda";
String lname = "Sahu";
int age = 25;
public void testing(){
System.out.println("This is a test Method!!!");
}
public static void main(String[] args){
//Accessing the Data of Class: Main
testing myObj1 = new testing();
System.out.println(myObj1.yourname);
System.out.println(myObj1.mnumber);
myObj1.justtest();
//Accessing the Data of Class: testing
myObj1.testing();
System.out.println(myObj1.fname);
System.out.println(myObj1.lname);
System.out.println(myObj1.age);
}
}
If you want to run in IDE, right click in middle of testing class and find Run -> Run Application it the dropdown menu and click it. Try to separate each class in individual files. So you must have at least two files: Main.java and testing.java. I strongly recommend you to use Camel Case for class names and respect coding standard (which mean change testing to Testing or even better to Test).
If not, You must change it in Manifest file or find run configuration in your IDE.
I was writing a code in which I print a statement "Hello World" but an error occur named cannot find symbol. I tried hard to remove this error but failed.
public class Input{
System.out.println("Hello World");}
This is the statement and the error
Please anyone can help me in resolving this error and tell me why this error occur so I will not repeat this mistake in future.
Use this :
public class Input {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
You can't print in the scope of a class. You need to inform more about the difference between a function and a class.
A "class" is sort of like a noun. It's the person, place or thing.
public class myThing {
//This is a comment
//put stuff that describes the thing in the curly braces
}
In order to make the thing do something, you must put it in a method
public void myMethod(){
//code in here gets run when myMethod runs
}
Methods must be in a class though. A method can usually be though of as "something a thing can do"
public class myThing {
public void myThingCanDoThis(){
//does this stuff only when called from somewhere else
}
}
The main method is a special type of method that always looks like this:
public static void main(String[] args) {
//This special method gets run automatically when your program runs
}
Your hello world should define a thing. It should have the method, or "verb", of "main".
public class myThing {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
For now just assume that all your java code must go inside of the main method. Learn the basics and then when you learn more start to pay attention to things like "public", "static", "class" , "void" etc. It will all make sense in time
//: innerclasses/TestBed.java
// Putting test code in a nested class.
// {main: TestBed$Tester}
public class TestBed {
public void f() { System.out.println("f()"); }
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t.f();
}
}
} /* Output:
f()
*///:~
I am studying "Think in Java". I am just wondering why the above code doesn't work which should be a way to test each class, and can be removed by deleting TestBed$Tester.class file.
The error msg instructs there should be a public static void main(String[] args) in TestBed class as program entry.
java compile version: javac 1.7.0_40
The main method must be in the public top-level class. That is the one with the same name as the java-file. Here, that's the TestBed-class.
The current main method is in an inner class (namely TestBed$Tester), and can't be used to start a program.
EDIT: I may have been wrong. I took a look in the book you mentioned, and it looks like you're able to run the inner class from the Command Promt by writing:
java TestBed$Tester
Was just testing a simple code, and it appears my eclipse just got worse. This code is suppose to output 2. But when I run it, very weird error says 'Error: Main method not found in class jasc1, please define the main method as: public static void main(String[] args)' when my main method is clearly defined.
Does anyone know what this error is all about??
public class jasc1 {
int a = 2;
public void abc(){
System.out.print(a);
}
public static void main(String[] args){
new jasc1().abc();
}
}
This works fine for me, your file name must be wrong. It MUST be the same as the class name.
Additionally
Class names should (by convention) begin with an upper case letter so Jasc1
If you want to execute a class It should have a method public static void main(String [] args) (or similar meanings). But, for executing a class you have to run that class.
ex: In command line you call java jasc1 after compiling it with javac jasc1.java (ofcourse there are some options like -cs; see help)
The same way you can run one class in Eclipse or NetBeans IDEs by right-clicking on it in the project explorer and select Run or Run as
Of course, this jasc1 class cannot call another class that have a public static void main(String []args) method.