main()如何在Java中调用非静态nethods?

Par*_*ras 1 java oop static

好.我是Java新手.我知道主要需要是静态方法.但我已经读过静态方法只能调用其他静态方法?那么为什么我们可以调用非静态方法呢?这是一个混乱而不是一个问题.例如

 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);
    }
    }
Run Code Online (Sandbox Code Playgroud)

在这里,main()是一个静态方法.那么,它必须只调用静态方法吗?怎么来,它可以调用calarea()?如果我是正确的创建一个对象?

编辑:

我曾经想过一样.我知道如何调用静态方法.我只想知道如果可以调用非静态方法(通过任何方式),那么为什么说静态方法只能调用其他静态方法?

Ste*_*n C 9

但我已经读过静态方法只能调用其他静态方法?

这可能是错误陈述或误读.

"规则"的更正确的陈述是静态方法在没有特定(非空)实例引用的情况下不能调用实例方法.或者换句话说,this在静态方法中无效,因此无法显式或隐式地使用它来进行方法调用.

您的示例不会破坏规则...以任何一种形式.它使用非空对象引用,并且未this显式或隐式使用.


大多数其他子问题都是由上述"提出"但......

如果我是正确的创建一个对象?

是.必须创建对象才能拥有可用于调用实例方法的对象引用.没有其他方法可以调用实例方法.