Tuesday, July 30, 2019

Instance Variables in Java

  1. class Test{
  2. int x = 10;
  3. public static void main(String args[]) {
  4. System.out.println(x); // C.E: Non static variable x can't be referenced from a static context
  5. Test t = new Test();
  6. System.out.println(t.x); //10
  7. }
  8. public void m1() {
  9. System.out.println(x);//10
  10. }
  11. }
  1. class Test{
  2. int i;
  3. boolean b;
  4. double d;
  5. String s;
  6. public static void main(String args[]) {
  7. System.out.println(i); // C.E: Non static variable x can't be referenced from a static context
  8. Test t = new Test();
  9. System.out.println(t.i); //0
  10. System.out.println(t.d); //0.0
  11. System.out.println(t.b); //false
  12. System.out.println(t.s); //null
  13. }
  14. }

0 comments:

Post a Comment