00:00
K02_a.png
K02_a.png
/* 01 - sT Var. In Cls. : WORKS FINE */ class K02 { int a =10; static int b = 20; void m1(){ int c =30; } public static void main(String[] args) { } }
/*02 - sT Var. In M/t. : ILLEGAL START OF EXPRESSION */ class K02 { int a =10; static int b = 20; void m1(){ static int c =30; } public static void main(String[] args) { } }
/*03 - sT Var. Can't Directly Access In Another cls.. : CANNOT FIND SYMBOL */ public class K02 { static int a = 20; } class book { public static void main(String[] args) { System.out.println(a); } }
/*04 - sT Var. Can Directly Access In Another cls. Using cls. Name. : 20*/ public class K02 { static int a = 20; } class book { public static void main(String[] args) { System.out.println(K02.a); } } // s/T v/R belongs to the class not the object
/*05 - Non sT Var. Can Not Access In Another sT cls. Using cls. Name. : NON-STATIC VAIRABLE A CANNOT BE REFERED FORM A STATIC CONTEXT */ public class K02 { int a = 20; } class book { public static void main(String[] args) { System.out.println(K02.a); } }
/*06 - employee obj. init. ( id,name,comp) using c/t , w/o using sT */ public class K02 { public static void main(String[] args) { Employee e1 = new Employee(101, "Rajat", "kb"); e1.display(); Employee e2 = new Employee(102, "Deepak", "kb"); e2.display(); Employee e3 = new Employee(103, "Manoj", "kb"); e3.display(); } } class Employee { int empid; String emp_name; String emp_comp; Employee(int empid, String emp_name, String emp_comp){ this.empid=empid; this.emp_name=emp_name; this.emp_comp=emp_comp; } void display(){ System.out.println(empid+" "+emp_comp+" "+ emp_name); } } // 101 kb Rajat // 102 kb Deepak // 103 kb Manoj
/*07 - employee obj. init. ( id,name,comp) using c/t , with sT comp. */ public class K02 { public static void main(String[] args) { Employee e1 = new Employee(101, "Rajat"); e1.display(); Employee e2 = new Employee(102, "Deepak"); e2.display(); Employee e3 = new Employee(103, "Manoj"); e3.display(); } } class Employee { int empid; String emp_name; static String emp_comp ="kb"; Employee(int empid, String emp_name){ this.empid=empid; this.emp_name=emp_name; } void display(){ System.out.println(empid+" "+emp_comp+" "+ emp_name); } } // 101 kb Rajat // 102 kb Deepak // 103 kb Manoj
/*08 - counter program using instance Var..*/ public class K02 { int count=0; K02(){ count++; System.out.println(count); } public static void main(String[] args) { K02 k1 = new K02(); // 1 K02 k2 = new K02(); // 1 K02 k3 = new K02(); // 1 K02 k4 = new K02(); // 1 } } // The static variable gets memory only once in the // class area at the time of class loading.
/*09 - counter program using sT Var.. */ public class K02 { static int count=0; K02(){ count++; System.out.println(count); } public static void main(String[] args) { K02 k1 = new K02(); // 1 K02 k2 = new K02(); // 2 K02 k3 = new K02(); // 3 K02 k4 = new K02(); // 4 } }
/*10 - non sT m/t call within cls using obj. */ public class K02 { void display(){ System.out.println("display method"); } public static void main(String[] args) { K02 k1 = new K02(); k1.display(); } }
/*11 - non sT m/t call within cls. w/o using obj. */ public class K02 { void display(){ System.out.println("display method"); } public static void main(String[] args) { display(); } } // non-static method display() cannot be referenced from a static context display();
/*12 - sT m/t call within cls. using cls. name and directly */ public class K02 { static void display(){ System.out.println("display method"); } public static void main(String[] args) { display(); // works K02.display(); // works } } // display method // display method
/*13 - cant not cal sT m/t by other cls. directly. */ public class K02 { static void display(){ System.out.println("display method"); } public static void main(String[] args) { pen(); } } class Book { static void pen(){ System.out.println("this is my pen"); } } // cannot find symbol pen();
/*14 - sT m/t call by other cls. using cls name, */ public class K02 { static void display(){ System.out.println("display method"); } public static void main(String[] args) { Book.pen(); } } class Book { static void pen(){ System.out.println("this is my pen"); } } // this is my pen
/*15 - sT m/t can only access sT data. */ public class K02 { int i =10; static void display(){ System.out.println(i); } } // non-static variable i cannot be referenced from a static context // System.out.println(i);
/*16 - sT m/t can call only sT m/t directly. */ public class K02 { int i =10; static void display(){ show(); System.out.println("hi"); } void show(){ System.out.println("hello"); } } // non-static method show() cannot be referenced from a static context
/*17 - sT m/t Cannot Refer To 'This' Or 'Super' Keyword In Anyway. */ public class K02 { int i =10; static void display(){ System.out.println(this.i);// can not use super also } } // non-static variable this cannot be referenced from a static context
/*18 - Only sT Block Executes Automatically When The Cls. Is Loaded In The Memory. */ public class K02 { static{ System.out.println("hello"); } } // Main method not found in class K19, please define the main method as: // public static void main(String[] args)
/*19 - Only sT Block With Main M/T sT Block Executes First Then Main m/t. */ public class K02 { static{ //1 System.out.println("hello"); } public static void main(String[] args) { //2 System.out.println("hi...."); } } // hello // hi....
/*20 - Multiple sT Block In cls., Executes Top To Bottom, sT Members Executes First. */ public class K02 { static{ // 1 System.out.println("static block first"); } public static void main(String[] args) { // 3 System.out.println("main block at last"); } static{ // 2 System.out.println("static block second"); } } // static block first // static block second // main block at last
Home PREVIOUS NEXT