简体   繁体   中英

how main() can call non-static nethods in Java?

Well. I am new to Java. I know that main needs to be static method. But I have read that an static method can call only other static methods ? Then how come we can call non-static methods ? Its a confusion rather than a question. For example

 public class Function
    {
    public static int side = 10,area,vol;

    public static void main(String args[])
    {
        System.out.println("programme to find area and volume");
        Function fu = new Function();
        fu.calarea();
    }
    public void calarea()
    {
        area = side*side;
        System.out.println("finished calculating area now calling volume");
        calvol();
    }
    public void calvol()
    {
        vol = area*side;
        System.out.println("finished calculating volume now calling display");
        display();
    }
    public void display()
    {
        System.out.println("side of a square ==>"+side);
        System.out.println("area of a square ==>"+area);
        System.out.println("volume of a square ==>"+vol);
    }
    }

In here, main() is a static method. So, it must call static methods only ? How come, it can call calarea() ? If I am right by creating a object ??

EDIT:

I had thought the same. And I know how to call static methods. I only want to know that if it is possible to call non-static methods(by any means), then why it is said that an static method can call only other static methods ?

But I have read that an static method can call only other static methods ?

That is either a misstatement or a misreading.

A more correct statement of the "rule" is that a static method cannot call instance methods without a specific (non-null) instance reference. Or to put it another way, this is not valid in a static method, so it cannot be used explicitly or implicitly to make method calls.

Your example doesn't break the rule ... in either form. It is using an non-null object reference, and it is not using this explicitly or implicitly.


Most of the other subquestions are "mooted" by the above but ...

If I am right by creating a object ??

Yes. It is necessary to create the object in order to have an object reference that you can use to call the instance method. There is no other way to call an instance method.

Yes by using instance you can call instance methods.

calarea() 

is instance method, so, you need to create an instance of Function class and call calarea() on that instance reference.

There are other flavours but this one should get it across.

if you have a class with mixture of static and instance methods, the static methods can't call the the instance ones. eg

in pseudo code

class SomeClass
{
static void IDoSomething()
{
   UDoSomething();  
}

void UDosomething
{
}
}

would give you an error but

class SomeClass
{
static void IDoSomething(SomeClass argSomeClass)
{
   argSomeClass.UDoSomething();  
}

void UDosomething
{
}
}

would be okay.

You are right. You need to create instance (object) of the class Function. So the right code is:

Function f = new Function();
f.calvol();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM