Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 23rd, 2006, 12:11 AM   #1
quantalfred
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 quantalfred is on a distinguished road
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?
quantalfred is offline   Reply With Quote
Old Jul 23rd, 2006, 12:42 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by quantalfred
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?
You can create other versions of main(), with different signatures. However, it is the static one (with a String array as its sole argument) that is called when you 'run' the class. There are two reasons for this.

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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jul 23rd, 2006, 1:02 AM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Jul 23rd, 2006, 1:30 AM   #4
quantalfred
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 quantalfred is on a distinguished road
Quote:
Originally Posted by Eric the Red
Please post what you have so far. Also, include the line number of where the error occurs.
public class test2{
	public void main(String [] Args){
		System.out.println("hi");
	}
}
in the file test2.java
and
public class test1{
	public static void main(String [] Args){
		test2 aa;
		String[] arg;
		aa.main(arg);
	}
}
in the file test1.java.

test2.java can be compiled but not executed. When test1.java is compiled, the error is what I just posted in my first post.
quantalfred is offline   Reply With Quote
Old Jul 23rd, 2006, 1:35 AM   #5
quantalfred
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 quantalfred is on a distinguished road
Quote:
Originally Posted by lectricpharaoh
You can create other versions of main(), with different signatures. However, it is the static one (with a String array as its sole argument) that is called when you 'run' the class. There are two reasons for this.

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.
Thanks, does that mean, the main function is special in the sense that it can not be called in other class. Or if it can be, how can I do it?
quantalfred is offline   Reply With Quote
Old Jul 23rd, 2006, 1:34 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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);
	}
}
titaniumdecoy is offline   Reply With Quote
Old Jul 23rd, 2006, 1:38 PM   #7
JimmyJim
Programmer
 
JimmyJim's Avatar
 
Join Date: Jul 2006
Location: England
Posts: 43
Rep Power: 0 JimmyJim is on a distinguished road
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. 
                 
        }
        ...
}
Calling main is a very bad idea if you ask me.

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.
JimmyJim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC