A nonstatic variable can be accessed through the static main method by creating an object of that class. how is this possible?
the object-oriented rule is non-static variable can not be accessed by static method because While we run a class, first static blocks/ static variable initialization happens. Static block gets executed when a class is loaded into JVM. so we can't access nonstatic variable inside executed static method. But why only this scenario no compilation error? please give me a explanation
This is allowed
public class Thread1 extends Thread{
int s ;
}
public class MainThread {
int y =89;
public static void main(String[] args) {
Thread1 j = new Thread1();
System.out.println(j.s=43);
}
}
This is not allowed why ???
public class MainThread {
int y =89;
public static void main(String[] args) {
Thread1 j = new Thread1();
System.out.println(y);
}
}
Thanks :)