class Employee{
int empId;
String empName;
String compName="DAV JavaServices Ltd.";
}
//Program of static variable
class Employee{
int empId;
String empName;
static String compName = "DAV JavaServices Ltd.";
Employee(int empId, String empName){
this.empId = empId;
this.empName= empName;
}
void display (){
System.out.println(empId+" "+empName+" "+compName );
}
public static void main(String args[]){
Employee employee1 = new Employee(111,"Dinesh");
Employee employee2 = new Employee(222,"Sweety");
employee1.display();
employee2.display();
}
}
//Program of changing the common property of all objects(static field).
class Employee{
int empId;
String empName;
static String compName = "DAV JavaServices Ltd.";
static void change(){
compName = "DOJ Ltd";
}
Student(int empId, String empName){
this.empId = empId;
this.empName = empName;
}
void display (){
System.out.println(rollno+" "+name+" "+college);
}
public static void main(String args[]){
Employee.change();
Employee employee1 = new Employee(111,"Dinesh");
Employee employee2 = new Employee(222,"Sweety");
Employee employee3 = new Employee(333,"Anamika");
employee1.display();
employee2.display();
employee3.display();
}
}
//Program of accessing non-static data member directly from static method main
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
}
}
/Program of static block
class A{
static{
System.out.println("static block is invoked");
}
public static void main(String args[]){
System.out.println("Hello main");
}
}
//Program of executing a program without main() method
class A{
static{
System.out.println("static block is invoked");
System.exit(0);
}
}
Labels: Core JAVA