How do you call a method from static main()?

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();
        }
    }
}

You can change the function as a static and call it . Thats all.


static class Program
{        
    [STAThread]
    static void Main()
    {
        string btchid = Program.GetCommandLine();
    }

    private static string GetCommandLine()
    {
        string s = "";
        return s;
    }
}

A linear search approach to your problem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinearSearch
{
class Program
{

    static void Main(string[] args)
    {
        int var1 = 50;
        int[] arr;
        arr = new int[10]{10,20,30,40,50,60,70,80,90,100};
        int retval = linearsearch(arr,var1);
         if (retval >= 1)
         {
           Console.WriteLine(retval);
           Console.Read();
         }
         else
         { Console.WriteLine("Not found"); Console.Read(); }
    }

    static int linearsearch(int[] arr, int var1)
    {
        int pos = 0;
        int posfound = 0;
        foreach (var item in arr)
        {
            pos = pos + 1;
            if (item == var1)
            {
                posfound = pos;
                if (posfound >= 1)
                    break;
            }     
        }  
        return posfound;
    }
}
}

Comments

  1. Carter

    • 2021/2/14

    you can call the method. If method is static, then there is no need to create object and you can directly call it followed by class name.

  2. Jeremiah

    • 2020/10/4

    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 (); } } } You can change the function as a static and call it . Thats all.

  3. Kelvin

    • 2016/1/6

    How can I make a function call from the Main method? I know the code below won't work static void Main(string[] args) 

  4. Cruz

    • 2021/1/28

    public static void main (String [] args) { int result; result = Math. min (10, 20); //calling static method min by writing class name System. out. println (result); System. out. println (Math. max (100, 200)); }}

  5. Leighton

    • 2019/4/19

    If the main method won't be static, JVM would not be able to call it because there is no object of the class is present. Let's see what happens when we remove 

  6. Nathanael

    • 2021/6/13

    We can call a static method by using the ClassName.methodName. The best example of the static method is the main() method. It is called without creating the object. In the following program, we have called the static method of the Math class, named min() that returns the minimum value of two numbers. StaticMethodCallExample.java

  7. Castillo

    • 2020/11/24

    Static methods are the method which invokes without creating the objects, so we do not need any object to call the main() method. void: In Java, every method 

  8. Vivaan

    • 2015/9/18

    After creating function, you need to call it in Main () method to execute. In order to call method, you need to create object of containing class, then followed by dot (.) operator you can call the method. If method is static, then there is no need to create object and you can directly call it followed by class name.

  9. Flores

    • 2018/1/24

    The static main() method makes it very clear for the JVM to call it for launching the Java Application. · The method is static because otherwise 

  10. Julio

    • 2020/11/17

    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.

  11. Ismael

    • 2018/12/3

    How to call 'public static void main(String[] args)' method from our code? These are some questions that usually puzzles a Java programmer.

  12. Harris

    • 2015/11/12

    How can I make a function call from the Main method? I know the code below won't work static void Main(string 

  13. Joe

    • 2020/12/24

    E.g. if you run java Helloworld than JVM will search for public static void main String args[]) method in HelloWorld.class file. 3. The main method is an 

  14. Joey

    • 2020/9/24

    Inside main , call the myMethod() method: public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void 

  15. Silas

    • 2020/10/13

    In the main method, change how you call the static methods by using just the classname instead of creating an object. Add a public static variable called 

Comments are closed.

Recent Posts