I have a console application with a Main
method and a function.
How can I make a function call from the Main
method?
I know the code below won't work
static void Main(string[] args)
{
string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string
}
To call a static method from another class, you use the name of the class followed by the method name, like this: ClassName. methodName().
Once a method is declared in a class it can be called in the main or any other method. There are also some built-in methods already defined in Java libraries. To call any built-in or self-defined methods using the syntax described in detail below.
Call a Method Inside main , call the myMethod() method: public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"
Static method of a class can be called by using the class name only without creating an object of a class. The main() method in Java must be declared public, static and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.
There's also
var p = new Program();
string btchid = p.GetCommandLine();
Make the GetCommandLine
static
!
namespace Lab
{
public static class Program
{
static string GetCommandLine()
{
return "Hellow World!";
}
static void Main(string[] args)
{
System.Console.WriteLine(GetCommandLine());
System.Console.ReadKey();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With