Day1 – OOPS

As I promised in my previous entry, I am gonna update my pogress regarding OCJP. Today, I studied about OOPS.

I made good number of flash cards on these concepts.

Lets get started

Static Control Flow

When we try to execute  a java class, first .class file would be loaded at the time of class loading the following action will be performed automatically

  1. Identification of static members from top to bottom. ([1] to [6]).
  2. Static variable assignments and execution of static blocks from first line to last ([7] to [12]).
  3. Execution of main method ([13] to [15]).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Example for illustrating static control flow */
 
class  StaticControlDemo
{
	static int x= 10; // [1] [7]
 
	static //[2] 
	{
		m1(); //[8]
		System.out.println("First Static Block"); //[10]
	}
 
	public static void main(String[] args) //[3]
	{
		m1(); //[13]
		System.out.println("Main method"); //[15]
	}
 
	public static void m1() //[4]
	{
		System.out.println(y); //[9] [14]
	}
 
	static //[5]
	{
		System.out.println("Second Static Block"); //[11]
	}
 
	static int y = 20; // [6] [12]
}
/* Output 
0
First Static Block
Second Static Block
20
Main method
*/

I know the above program is bit confusing, I will try to elucidate more

At first all static members of the class are identified (just identified , no assignment of variables and execution of static blocks is done), observe [1] to [6] . After identification variable assignment and execution of static blocks starts from top to bottom so value 10 is assigned to int x[7] , the next static block is executed here m1() method is called , observe  m1() is trying to print y which is not yet initialized so the default value “0” is printed, now control flows back to static block and the next statement  “First Static Block”[10] is displayed on console. After it the second static block is executed and “Second Static Block” is printed[11], now the y is initialized[12]. Next the main method is executed [13] to [15].

 

Static Control Flow from Parent to Child

  1. Identification of static members from Parent to Child. ([1] to [11]).
  2. Static variable assignments and execution of static blocks from Parent to Child ([12] to [22]).
  3. Execution of child class main method ([23] to [25]).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Example for illustrating static control flow in parent to chil */
 
class  Parent
{
	static int x= 10; // [1] [12]
 
	static //[2] 
	{
		m1(); //[13]
		System.out.println("Parent Static Block"); //[15]
	}
 
	public static void main(String[] args) //[3]
	{
		m1(); 
		System.out.println("Main method"); 
	}
 
	public static void m1() //[4]
	{
		System.out.println(y); // [14]
	}
 
	static int y = 20; // [5] [16]
}
 
class Child extends Parent
{
	static int i = 100 ; //[6][17]
 
	static //[7]
	{
		m2();//[18]
		System.out.println("Child First Static Block"); //[20]
	}
public static void main(String[] args) //[8]
	{
		m2(); //[23] 
		System.out.println("Child Main method"); //[25]
 
	}
	public static void m2() //[9]
	{
		System.out.println(j); //[19][24] 
	}
 
	static //[10]
	{
		System.out.println("Child Second Static Block"); //[21]
	}
	static int j = 200 ;//[22]
}
/* Output 
0
Parent Static Block
0
Child First Static Block
Child Second Static Block
200
Child Main method
 
*/

Program to understand more about constructors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Animal 
{
	String name;
	Animal (String name) {
		this.name = name ;
	}
 
	Animal()
	{
		this (makeRandomName());
	}
	static String makeRandomName()
	{
		int x= (int) (Math.random() * 5);
	    String name = new String[] {"Fluffy", "Fido", "Rover" ,"Puppy","Snoopy"} [x] ;
			return name;
	}
 
	public static void main(String [] args)
	{
		Animal a = new Animal();
		System.out.println(a.name);
		Animal b = new Animal("Zeus");
		System.out.println(b.name);
	}
 
	}

Program to understand about the “static”  keyword

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Frog 
{
	static int frogCount = 0;
 
	public Frog()
	{
		frogCount +=1;
	}
}
class TestFrog
{
	public static void main(String[] args) 
	{
      new Frog();
	  new Frog();
	  new Frog();
		System.out.println("Frog Count" +Frog.frogCount);
	}
}

I skipped explanations for programs which are fairly simple and concepts which you can easily understand them by using Kathy Sierra book.

If you have any doubts please leave a comment.

 

Leave a Reply