Why does this matter? Well, there could potentially be thousands of instances of Main, so what instance should the method run on? The compiler has no way of knowing. You can write anything you like in comments they dont affect the logic of the software in any way. As the name implies, comments are simply text written by programmers during the creation of code. test() needs to be static as well, otherwise it belongs to instances of Main and not the Main class. Comment line: // c++ hello world program. The third problem is that test is an instance method but you're calling it statically. If you don't do this then the compiler has no way of knowing whether what you wrote was meant to be a method or a constructor. You always need to specify a return type, in this case nothing is returned so the type is void. The second problem with this is that there's no return type specified. Since you're only calling test from the main method here, and you're passing a string to it, you may as well set the type of args to String. As to what the type is here, () can actually take anything, so you could set the type to String, Object or whatever you like (since Object has a toString() method and it has lots of overloads to deal with all the primitives.) Bear in mind this is unusual though, most of the methods you come across will just take something of a specific type! You need a type to go with a parameter - Java is a strongly typed language and thus you always need to specify a type.
0 Comments
Leave a Reply. |