|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Homework - Public Static Void Main()
I'm new to Java, and I was under the impression, led to believe that all Java programs begin with this:
Public Static Void Main() However, I have seen many java programs that do not have this, which is confusing. Can someone tell me the circumstances that require this in a Java file? How do Java program run without this? |
|
#2
|
||||
|
||||
|
Applets don't require a "public static void main (args[])". Classes also do not require it. It is only necessary if you plan on running your application as a program (hence, if its not an applet, it IS required).
__________________
"Java makes impossible things possible, but makes easy things difficult." - Somebody
|
|
#3
|
||||
|
||||
|
Quote:
Or a web service. Or an EJB. Or a web application. Or any other type of application designed to be run inside a container or as a plugin (much more than just applets). ~
__________________
Yawmark class Sig{public static void main(String...args){\u0066or(int \u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray() )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>> +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}} |
|
#4
|
|||
|
|||
|
The only Java programs that contain Public Static Void Main() the ones that actually run.
Since you can have many classes in a Java program, the one that actually runs the program is the one that will have the Public Static Void Main() in it. The others won't, but they are accessed by the Main program. When you first start out learning Java, they give you short little programs to introduce you to the various parts and they all have the Public Static Void Main() in them. Then later, you get introduced to the whole point of the object oriented programming and you have various other classes that don't have it in them. Can be confusing at first. Make sense? hope that helps |
|
#5
|
||||
|
||||
|
Quote:
Not quite. The "public static void main(String[] args)" method can be declared in any class or classes. Just because a class has that method does not mean that class must be run; only that it can be run. It's the method called by the JVM on the class designated at startup time. Aside from that, it's a method just like any other. ~ |
|
#6
|
|||
|
|||
|
Quote:
OK. Reason why I am asking this is I am trying to build a J2ME application for Bluetooth using MIDlets, basically sending/receiving data between my laptop/mobile phone. In order to evaluate data sent from my mobile, I need to run stats on data received on my laptop server. When I receive data back from my mobile, it is stored in a data buffer in my USB Bluetooth Adaptor. Can I assume that a Java app (one that resdes on my laptop server) will be able to access this data buffer via an API. Is my thinking correct? |
|
#7
|
||||
|
||||
|
Quote:
I forgot/didn't know about half of those. I guess I have never had a reason to look into them. Maybe I should for the sake of knowledge? Anyway... Quote:
Last edited by tagmanadvance : January 2nd, 2008 at 08:37 PM. |
|
#8
|
|||
|
|||
|
Quote:
If you want to play a game of 'Theory', yeah, definitely possible... Honestly speaking though, I don't think you'd have anywhere near the experience to try attempting that yet. I have no experience with bluetooth, but chances are you'd have to find out where the data gets sent and retrieve it or in worse case scenario, reverse-engineer the drivers packaged with the device... and not even mentioning the fact that it's not going to be written in java, but doing all that is not going to be easy. So the first couple of things on your to do list is: A) Figure out how your computer communicates with the device. B) Recode/Convert all of the algorithms you need in java, which may not necessarily be possible. C) Code your own interface, so your program can understand the input. EDIT::: If it stores itself in a databuffer, and you know where this is in memory, you can try to retrieve it... However, I've no idea how to do this. I'm talking completely out of my ***, but my guess is it's going to be awhile before you're going to be interfacing yourself with those things. |
|
#9
|
||||
|
||||
|
Quote:
let me help you understand what those properties mean: public - this means other objects can access this classes data or access the method from outside the class. The alternative of this is "private" which means only other methods in the same class can call the method (kind of irrellevant for main, it's never really called again). static - This means that theres only one copy of the thing your declaring static in memory and you don't need to create an instance of the class. It's generally done because when the main method is executed you haven't actually created an instance of the main class. When the variables in a class are static if you create multiple instances of the class then they will share the same variables (alter one objects variable and you alter them all). A very useful but confusing feature of OOP .Void - this is the methods return type. If the method is called from inside the class or outside or whatever, the caller may expect a value returned. Like when you call a mathematical function from a class, lets say some made up method like MathsClass.add1and2() returns an integer type (a number, 3). Void simply means it returns nothing. A main method is never called (or at least it shouldn't ) so void is usually used. This is confusing since languages like C (java is kind of a derivative of C) used to return 1 or 0 depending on whether an error occured.Last edited by calpol2004 : January 3rd, 2008 at 08:02 AM. |
|
#10
|
|||||||
|
|||||||
|
Quote:
Outside the package or subclasses. Protected and package-private (i.e., default access) members can also be accessed outside the class. Only the private modifier restricts access to within the class. Quote:
For the JVM to call the main() method of a class at startup, the method must be declared static. This is done out of convention and so the JVM doesn't need to instantiate the class. Quote:
Sure it is. Quote:
There is no general reason why it shouldn't be called. Once again, a "public static void main(String[] args)" method is just like any other public, static method that takes a string array as its only parameter and returns void. There may be a perfectly good reason to call main(); e.g., an application wrapper calling an underlying program, etc. Quote:
For the main() method to be called by the JVM at startup, it must return void. Also note that frequency with which a method will be called is orthogonal to whether or not that method returns (or should return) void. ~ Last edited by Yawmark : January 3rd, 2008 at 11:27 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Public Static Void Main() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|