New course! Every coder should learn Generative AI!
Try a free lesson0
Accessing static methods
Can we access static methods from another object after using destructor on the previously initialized object of that class?Does this help resolve the referrence from non-static context error? https://code.sololearn.com/cUMoSA5Tzu5z/?ref=app
1 Answer
- 1
// how to solve non-static error
public class Program {
int i;
public static void main(String[] args) {
// System.out.println(i); //static error
// way 1
var p = new Program();
System.out.println(p.i);
// way 2
new Program().main2();} void main2(){
System.out.println(i);
}
}