Im having issues now with static and non staic errors. with sertain variables unable to find the main method.
I have strated the program by creating a seperate file which instantiates the class. like so:
public class StartUp {
public void main(String[] args) {
MainDriver theMainDriver = new MainDriver();
theMainDriver.start();
}
}
Within certain classes in the program it passes variables back to the mainDriver. But when I try to refrence it to, I get the error "cannot find symbol variable theMainDriver" .
e.g:
public void getEmployee() {
theMainDriver.setEmployee(theEmployee);
}
public void getEmployeeID() {
theMainDriver.setEmployeeID( randomIDno);
}
how can I declare the main Driver in a way that makes it more visible to other classes.
It does find the main driver if I do this MainDriver.setEmployeeID( randomIDno); but then it has issues with non static method cannot be referenced from a static context.
You can do the following:
public class StartUp {
public static MainDriver theMainDriver;
public void main(String[] args) {
theMainDriver = new MainDriver();
theMainDriver.start();
}
}
And call from any class as follows:
Startup.theMainDriver.setEmployeeID(randomIDno);
3 things if there was a compilation error.
1) Is MainDriver Public?
2) if MainDriver is in different package, did u import it ?
3) If MainDriver is from diffrent pacakage or different project or a external jar, Did u give that in the classpath ?
also, as the othe guy said, u for got the static part of the main(String[] args)
Firstly, the declaration of main should be like
public static void main(String[] args)
I cant understand how you are running the programm without making it static
Secondly, You didn.t gave the structure of your MainDriver class. May be it is not public. So make your MainDriver class public. I think that will resolve the problem
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.
//: 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
I compiled this code and then, TestInner$1.class emerged.
I know ~~~$1.class indicates that file has "anonymous class."
But I don't understand the reason why this class file made. I want to know the reason.
Here is the code.
public class TestInner {
private static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I tried another version removed "private" identifier, like the following.
public class TestInner {
static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I'd imagined that this code also would make TestInner$1.class file.
However it didn't create the file.
In addition, the following code, added Constructor, also didn't make TestInner$1.class.
public class TestInner {
private static class Inner {
Inner(){ }
}
public static void main(String[] args){
new Inner();
}
}
I have no idea, so can anyone help me?
EDIT:
I found the same question and it solved. Thank you for your helping.
Why is an anonymous inner class containing nothing generated from this code?
None of your examples have anonymous inner classes. None of them will produce a file named TestInner$1.class. All of them will produce a file named TestInner$Inner.class.
The following example shows an anonymous inner class and will produce TestInner$1.class:
public class TestInner {
public static void main(String[] args){
new Object() {
#Override public String toString () {
return "ninja";
}
};
}
}
I'm not sure where your TestInner$1.class came from but I'm guessing it's left over from previous experiments you were doing.
Update 1: I can confirm that without using Eclipse I get TestInner$1.class (in addition to TestInner$Inner.class -- 3 files are produced) for the first example but not for the last two, just like you are seeing. Will update when I find out why. When compiled via Eclipse, TestInner$1.class is never produced.
Update 2: OP found solution in Why is an anonymous inner class containing nothing generated from this code?.
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.
I was trying to know what would happen if I have two classes with the main function.
I used the following code:
class A {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
class Hello {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
I compiled it using javac First.java (since no class is specified as public , I named the file as First.java); it got compiled without any error and i ran only the class A.Expecting the Hello class to run itself. DIDN'T HAPPEN(?),maybe the program ran out of scope.
So,
I tried compiling the following java code(I am a beginner) but i got the following error.
Code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello,World!");
}
}
class A {
public static void main(String[] args) {
System.out.println("Hello,World!");
Hello.main();
}
}
I compiled it through javac First.javaand got the following error:
method main in class Hello cannot be applied to given types;
Hello.main();
^
I wanted the program to run first the class A's main function and then class Hello's.
What's going wrong here?
Look at the declaration of Hello.main:
public static void main(String[] args)
Now you're trying to call it like this:
Hello.main();
What would you expect the value of args to be, within the method? You need to provide it with a value for args... and fortunately, you already have one, as you're within a method which uses args as a parameter, also of type String[]. So you should just be able to change your code to:
Hello.main(args);
Note that the two args parameters - one for Hello.main and one for A.main are entirely separate. We happen to use pass the value of one to the provide the initial value for the other, but we could easily have written:
Hello.main(null);
or
Hello.main(new String[] { "Some", "other", "strings" });
instead.
Do this in your second class:
public static void main(String[] args) throws IOException {
MyOtherClass.main(args);
}