Skip to main content
deleted 29 characters in body
Source Link
resueman
  • 10.7k
  • 10
  • 34
  • 46

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.

class A

{

class A { void method()     {     } 

} class Demo

{

} class Demo {  static void method2()     {     A a=new A();   a.method(); 

} /*

 }  /*  void method3() {   A a=new A();   a.method(); } 

*/

 */  public static void main(String args[])     {     A a=new A();   /*an instance of the class is created to access non-static method  from a static method */     a.method();     method2();     /*method3();it will show error non-static method can not be accessed from a static method*/     } } 

}

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.

class A

{

void method()   {   } 

} class Demo

{

 static void method2()   {   A a=new A(); a.method(); 

} /*

 void method3() { A a=new A(); a.method(); } 

*/

 public static void main(String args[])     {     A a=new A();   /*an instance of the class is created to access non-static method  from a static method */     a.method();     method2();     /*method3();it will show error non-static method can not be accessed from a static method*/     } 

}

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.

class A { void method()   {   } } class Demo {  static void method2()   {   A a=new A();   a.method();  }  /*  void method3() {   A a=new A();   a.method(); } */  public static void main(String args[]) { A a=new A(); /*an instance of the class is created to access non-static method from a static method */ a.method(); method2(); /*method3();it will show error non-static method can not be accessed from a static method*/ } } 
Source Link
Sourav Saha
  • 173
  • 2
  • 12

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.

class A

{

void method() { } 

} class Demo

{

 static void method2() { A a=new A(); a.method(); 

} /*

 void method3() { A a=new A(); a.method(); } 

*/

 public static void main(String args[]) { A a=new A(); /*an instance of the class is created to access non-static method from a static method */ a.method(); method2(); /*method3();it will show error non-static method can not be accessed from a static method*/ } 

}