package com.codegym.task.task06.task0616;
/*
Minimum number of statics
*/
public class Solution {
public static int step;
public static void main(String[] args) {
method1();
}
public static void method1() {
method2();
}
public static void method2() {
new Solution().method3();
}
public void method3() {
method4();
}
public void method4() {
step++;
for (StackTraceElement element : Thread.currentThread().getStackTrace())
System.out.println(element);
if (step > 1)
return;
main(null);
}
}
Can i access static variable from non static function. My below program is working but i have confusion for method 4 accessing a static variable.
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
DavidJugaad
3 August 2021, 18:12
On one hand, non-static methods can access any static method and static variable, without creating an instance of the object.
On the other hand, to access a non-static variable from a static method you must create an object of the class the variable belongs to.
To refresh some ideas:
https://codegym.cc/quests/lectures/questsyntax.level06.lecture06
Hope it helps
0