![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
Call the class main function
I define a class and write the main method without static. I can compile it. But in other class when I initiate one instance of that class (call it aa) and call aa.main(args); (with args the correct input type), I got an error "variable aa might not have been initialized.
I understand that main is called when it is executed. Then does that mean public static is the only way all the main functions can be, at least meaningfully? |
|
|
|
|
|
#2 | |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 927
Rep Power: 4
![]() |
Quote:
First, it needs to have the same 'signature', or argument list. This is so the Java VM knows what parameters to use when calling it. Second, and more importantly, it needs to be static so that the VM can call it without first creating any class instances. Non-static methods can only be called using an object of the class. Since the VM won't create classes until you tell it to (barring some startup classes it might create for its own use), you won't have an object to use before main() is called, and thus, it must be a static method. Having said all that, there is no absolute requirement that a given class have a main() method in the given format, or even a main() method at all. Instead, only the class that is the entry point for your application needs such a method.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Please post what you have so far. Also, include the line number of where the error occurs.
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
Quote:
public class test2{
public void main(String [] Args){
System.out.println("hi");
}
}and public class test1{
public static void main(String [] Args){
test2 aa;
String[] arg;
aa.main(arg);
}
}test2.java can be compiled but not executed. When test1.java is compiled, the error is what I just posted in my first post. |
|
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
lectricpharaoh has explained this very well, so I won't go into detail. To put it simply, when you run your Java application, the interpreter looks for a main method which must have the exact signature, public static void main(String[] args). This method will be the entry point for your program; any similar methods (eg, without the "static" keyword) will be treated as ordinary methods.
As for your example code, the reason it does not run has nothing to do with the naming of your methods. Since you have omitted the "static" keyword in the method main() in the test2 class, it is treated as an ordinary method. The errors you are experiencing arise from not having initialized the class or array variables; remember that all variables in Java are references. public class test1{
public static void main(String [] Args){
test2 aa = new test2();
String[] arg = {};
aa.main(arg);
}
} |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jul 2006
Location: England
Posts: 43
Rep Power: 0
![]() |
you can call main like this
class MyClass
{
public static void main(String[] args)
{
MyClass m = new MyClass();
m.main(args) ;
//note this wont work, as main is static and therefore is not part
//of the instance of MyClass m.
MyClass.main(args);
//this should work as you can access static methods and fields
//on a class by using the class name.
//NOTE: THIS IS RECURSIVE IN MY EXAMPLE.
}
...
}Its static so that there does not have to be an instance of MyClass to call main. Static method and fields are never part of a instance of that class, but are always there if you call them by the class name. So you could have a static variable that keeps track of the number of instances of that class by incrmenting it in the constructor and decrementing it in the finalize method. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to call normal "write" function inside a class | Edgar | C++ | 1 | May 24th, 2006 6:35 PM |
| Little help | whoawhoayoyo | Assembly | 8 | Apr 18th, 2006 7:10 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| OOT Program Examples | Sane | Other Scripting Languages | 4 | Nov 25th, 2005 12:06 AM |
| Jackpot game | zorin | Visual Basic | 3 | Jun 10th, 2005 1:19 PM |