EELU OOP Week5
EELU OOP Week5
class MyClass{
final int weight=90;//final variable
class Bike{
final void run(){System.out.println("running");}
}
class A{
A(){
this('a');
System.out.println("Default constructor");}
A(char c){ System.out.println("char");}
A(int x){
this(); // call default constructor
System.out.println(x);
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}
}
Passing
current object as an argument to
a method.
class Student{
void m(Student obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
Student s1 = new Student();
s1.p();
}
}
class A{
A getA(){
return this;
}
void msg(){System.out.println("Hello java");}
}
class Test1{
public static void main(String args[]){
new A().getA().msg();
}
}
A static method can access static data
member and can change its value.
The static method can not use non static
data member or call non-static method
directly.
this and super cannot be used in static
context.
Is used to initialize the static data
member.
It is executed before the main method at
the time of class loading.
class A2{
static{
System.out.println(“Loading class…");
}
class B2 extends A{
B2(){
super();
System.out.println("child class constructor invoked");
}