Results 1 to 5 of 5
  1. #1
    bob_bee is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default calling method from main method

    Hi, I have a problem with my code in calling another method from main method in the same class.

    sample code:

    class MyClass
    {
    public static void main( String[] args )
    {
    //call ProcessCommand();
    }
    public void ProcessCommand()
    {
    //statement;
    }
    }

    I tried to use this.ProcessCommand but got a compilation error;

    MyClass.java:5: non-static variable this cannot be referenced from a static context
    this.ProcessCommand();

    Any help is highly appreciated. Thanks much.

    Bob
    Last edited by bob_bee; 09-24-2009 at 01:23 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    instantiate your class and call it using the instantiated instance, or, if applicable, make it static.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    The method main is a static method. In short, when a method or variable is declared static, you don't have to instantiate the class it's in to call/use the method/variable.
    For example, you can do this:
    Java Code:
    public class Class1
    {
        public static void main(String[] args) {
            Class2.doSomething(); // Works, because doSomething is static.
        }
    }
    public class Class2
    {
        public static void doSomething() { // Static, can be called at anytime, without having to actually instantiate the class.
            // Code
        }
    }
    But you cannot do this:
    Java Code:
    public class Class1
    {
        public static void main(String[] args) {
            // Class2.doSomething(); // Does not work; doSomething() is not declared static so you first would have to instantiate the class. So this would work:
            (new Class2()).doSomething(); // Instantiate and call the method.
        }
    }
    public class Class2
    {
        public void doSomething() {
            // Code
        }
    }
    The same with method-to-method: a static method cannot call a non-static method or use a non-static variable, because that method actually isn't declared yet. The compiler recognizes that and 'blocks' it, to prevent runtime errors.

    To fix it, you should instantiate the class, or make your method static, so either use:
    Java Code:
    class MyClass
    {
        public static void main( String[] args )
        {
            processCommand();
        }
    
        public static void processCommand() // Made the method static
        {
            //statement;
        }
    }
    This is not recommended, because you could only have one instance (by which i mean, you can instantiate it multiple times, but all static variables and methods will be the same for each class, so pretty useless) of the class using this method.
    The other way to fix it is doing this:
    Java Code:
    class MyClass
    {
        public static void main( String[] args )
        {
            new MyClass(); // Instantiate your class
        }
    
        public MyClass() { // Not static
            processCommand(); // Call a non-static method from a (non-static) constructor (constructors can't be static)
        }
    
        public void processCommand()
        {
            //statement;
        }
    }
    Hope this helps.

    P.S.: Follow the coding conventions - start method names always with lower case letters.
    Last edited by Supamagier; 09-24-2009 at 03:55 PM.
    I die a little on the inside...
    Every time I get shot.

  4. #4
    bob_bee is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi masijade and supamagier. thanks for your replies. I understand now static method and variable. Thanks for the illustration and sample codes, also for the tips in coding convention. More power to you.

  5. #5
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Just want to jump in here and thank Supamagier. had trouble calling a method. your ex

    (new ocean()).newData(); Made that trouble go away. Thanks

Similar Threads

  1. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 01:37 PM
  2. Need help calling from a different method
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 09:27 PM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •