Java-Access nonstatic Members From Static Method

Access nonstatic Members From Static Method

A static method cannot call a non-static members from it. The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. The most common example of a static member is main( ). main() is declared as static because it must be called before any objects exist.

Program

1

Program Source

public class Javaapp {

    static void staticMethod()
    {
        System.out.println("Satic Method");
    }
    
    void nonStaticMethod()
    {
        System.out.println("nonStatic Method");
    }

    public static void main(String[] args) {
        
        staticMethod();
        
        Javaapp jp = new Javaapp();
        jp.nonStaticMethod();
    }
}

Leave a Comment