Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
java eclipse main method and static class?
hello, friends
i have two problems, the first time i use java eclipse
1)the main() method cannot be invoked that is nested in that demo class Helloworld i created for the first time
2)the class declaration do not support the static, private and protected keyword
what is the problem
thanks for comment
2 Answers
- Anonymous5 years ago
Firstly the correct object oriented way of calling a method (in this current block of code) is this.passMethod(x); But the variable x has been declared outside passMethod's scope, so that method doesn't know it exists. Try this: public class PassPrimitiveByValue { int x = 3; public static void main(String [ ] args) { this.passMethod(x); System.out.println("The value of x is: " + x); } public static void passMethod(int p) { //Printing parameter p value. System.out.println("value before change:"+p); p = 10; System.out.print(p); System.out.print(x); } }
- SilentLv 710 years ago
Well, like the error message says, a top-level class can't be static, private or protected. Since you mention "static class" in your question, I'm assuming you have the word static in your class definition. Remove it. Static outer classes do not exist in Java.
As for the first one, it's probably because your main() method doesn't have the right signature. To work properly, a main method in a Java class needs to be declared like this:
public static void main(String[] args) {
// do stuff
}
If you want more specific help, you'll need to actually show us the code you're working on.