Sunday, July 3, 2016

Strings in switch Statements

Since: JDK 1.7

public class Stringjdk7 {
      public static void main(String[] args) {
            String str = "paras";

            switch (str) {
            case "paras":
                  System.out.println("Name found");
                  break;
            default:
                  throw new IllegalArgumentException("Illegal argument" + str);
            }
      }
}

Output: Name found

public class ABCStatic {
      int a;

      public static void main(String[] args) {
            System.out.println(a);
      }
}

Can’t make a static reference to the non-static reference a

No, you can't access non static variable in static context because static context are loaded in memory when the class is loaded where as non static variables are instance variables and are created only when object is created

No comments:

Post a Comment