Mahmoud Metwaly

Greenhorn
+ Follow
since Jul 16, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mahmoud Metwaly

Great to hear about the new book! Welcome Khalid and Rolf!

The problem in preparation for SCJP IMHO is that many programmers I believe need more and more examples in the preparation books (something like those we use in real life) to have a better and deeper understanding on each of the things we learn.

So will the new book contain enough code examples especially in the area of Generics and Collections to help its readers have a better understanding?

Thanks in advance for your time

I am trying to compile the following code which have a Synchronized block in Inner class InnerClassE, although I always get a compiler error, so can someone please tell me what I am doing wrong here?

I am trying to test having a synchronized block in one of the deep inner classes while having the lock on this deep inner class and not on another object



Thanks in advance for your time
Assuming we have the following method



Now by exiting this method, how many objects are eligible for garbage collection 4 OR 5 (if we are going to count 4 objects of the array + the array object itself)?


Thanks in advance
I've found the following question in one of the mock up exams where the correct answer is strange for me



Now the correct answer for this question is that it Prints 5
while I think that it should Prints 0 since the method called during runtime is the method of the reference type (SuperTest) here

So am I thinking correct here or am I missing something?

Thanks in advance for your help
hi all,

I am not sure I am getting the idea of how synchronized static methods. synchronized blocks and synchronized static blocks works correctly

For synchronized static methods, as far as I understand every class has its own lock through its corresponding instance of java.lang.Class...which means that no matter how many objects do exist for the class that do have the synchhronized static methods, only ONE lock can be done at a time on the synchronized static methods of the class through its corresponding instance of java.lang.Class

Now what I am confused about in the classes which contains synchronized static methods or to be more specific in classes that contain synchronized static methods, synchronized methods, synchronized static blocks AND synchronized blocks in ONE class...what will happen for example if a thread gained lock on the synchronized static method of this class...can other threads access the synchronized methods, synchronized static blocks, synchronized blocks of this same class?


For instance and static blocks, I really don't understand how their blocking mechanism work, also the object on which the lock will happen so I will appreciate it so much if someone can please help me by explaining those two parts as well

Thanks in advance for your time and efforts
Thanks Ralph so much for your help
I had a typo mistake in the previous code so here is it again, and the results are still the same

<blockquote>code:
<pre name="code" class="core"> public class DogVer3 extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

int z = g(99);
{ System.out.println("6_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

int g(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

DogVer3(){
super();
System.out.println("SUB Const.");
dogConst();
}
static void dogConst(){
System.out.println("SUB Const. II");
}

public static void main(String[] args){
new DogVer3();
}
}

class Animal {

int iA = fA(1);
{ System.out.println("2_SUPER"); }

int jA = fA(3);
{ System.out.println("4_SUPER"); }

int zA = g(99);
{ System.out.println("6_SUPER"); }

static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);

int fA(int k){
System.out.println(k + "_SUPER");
return k;
}

int g(int k){
System.out.println(k + "_SUPER");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}

Animal(){
System.out.println("Super Const.");
animalConst();
}
static void animalConst(){
System.out.println("Super Const. II");
}

}
</pre>
</blockquote>
Ralph, thanks again everything is clear for me now Just one last question please

When I try to execute the following code

<blockquote>code:
<pre name="code" class="core"> public class DogVer3 extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

int z = g(99);
{ System.out.println("6_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

int g(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

DogVer3(){
super();
System.out.println("SUB Const.");
dogConst();
}
static void dogConst(){
System.out.println("SUB Const. II");
}

public static void main(String[] args){
new DogVer3();
}
}

class Animal {

int iA = fA(1);
{ System.out.println("2_SUPER"); }

int jA = fA(3);
{ System.out.println("4_SUPER"); }

int zA = g(99);
{ System.out.println("6_SUPER"); }

static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);

int fA(int k){
System.out.println(k + "_SUPER");
return k;
}

int g(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}

Animal(){
System.out.println("Super Const.");
animalConst();
}
static void animalConst(){
System.out.println("Super Const. II");
}

}
</pre>
</blockquote>

I get

instatic SUPER
5_SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1_SUPER
2_SUPER
3_SUPER
4_SUPER
99_SUBClass
6_SUPER
Super Const.
Super Const. II
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass
99_SUBClass
6_SUBClass
SUB Const.
SUB Const. II

Why 99_SUBClass did appear (Twice)? I mean why it was called twice here?

Thanks in advance for your time and efforts

Originally posted by Ralph Jaus:
Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors of the super class the overriden method is called.

But what exactly are you confused about ?

[ July 17, 2008: Message edited by: Ralph Jaus ]



Let me see if I understand you correctly, if I have class Animal and its sub-class Dog, where Dog 'override' all instant variables, methods and blocks of class Animal...

So when I initialize Dog, will this mean that the instant variables, methods and blocks of class Dog (which are already an overridden instant variables, methods and blocks of class Animal) will run first?

This is what I am exactly confused in, I am not sure what happens when there is an overridden (Instant variable OR block OR method)...what will its sequence in initializing in the sub-class AND the super class

That is exactly what I am confused about here

Originally posted by Ralph Jaus:

The output shown here is due to overriding the method f. Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors the overriden method is used.
[ July 17, 2008: Message edited by: Ralph Jaus ]



Thanks Ralph for your help and kind words...

Kindly can you elaborate the above paragraph as I am still confused here about how overriding did effect the initialization sequence in the code?

Thanks in advance for your time and efforts
First, thanks all for your help and support..I highly appreciate it

I have modified the code posted in one of the posts above a little as shown below

<blockquote>code:
<pre name="code" class="core"> public class Dog extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

public static void main(String[] args){
new Dog();
}
}

class Animal {

int iA = fA(1);
{ System.out.println("2.SUPER"); }

int jA = fA(3);
{ System.out.println("4.SUPER"); }

static { System.out.println("instatic SUPER"); }
static int mA = fstaticA(5);

int fA(int k){
System.out.println(k + ".SUPER");
return k;
}

static int fstaticA(int k){
System.out.println(k + ".SUPER_Static_VAR");
return k;
}

}
</pre>
</blockquote>

and by executing

java Dog

I got

instatic SUPER
5.SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1.SUPER
2.SUPER
3.SUPER
4.SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass


which confirms that all static variables, blocks for all classes in the inheritance tree of the first initialized class (Dog) will run first, then instance variable and blocks will follow from the top down in the inheritance tree

So far so good, but when I tried changing the code again and making it as follows:

<blockquote>code:
<pre name="code" class="core">
public class Dog2 extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

public static void main(String[] args){
new Dog2();
}
}

class Animal {

int i = f(1);
{ System.out.println("2_SUPER"); }

int j = f(3);
{ System.out.println("4_SUPER"); }

static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUPER");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}

}
</pre>
</blockquote>

and by executing

java Dog2

I got the following result:

instatic SUPER
5.SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1.SUPER
2.SUPER
3.SUPER
4.SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass

So I was wondering, shouldn't inheritance have played a role here especially that there are static and instance methods and vars with the SAME EXACT name?

Thanks in advance for your help
Simply because class String is descendant from class Object like any other Java class, and class Object have toString method which is used to provide string representation of any object (when needed)

I hope this helps

Originally posted by sweety sinha:

static init block executed at class loading time

instance init block runs after call to super constructor

when multiple init block of a single type occur in a class they run in order from top down

i hope this will help you




Thanks Sweety for your help...so just to make sure I understood you correctly

1- Static initialization blocks of the Animal class will run first in the order from top down

THEN

2- Instance initialization blocks of the Animal class will run in the order from top down

THEN

3- Instance variables of the Animal class will run in the order from top down

THEN

4- Animal class constructor

THEN

5. The exact same sequence happens with the Dog class

Am I right here?

and if yes, what if Animal and/or Dog class contains Static or Final variables...when Static or Final variables will be initialized in the above sequence?

Thanks in advance for your help
Hi All,

can someone please tell me the exact sequence of initialization in term of what gets initialized before what, if I have for example 2 classes say...

class Animal {}

class Dog extends Animal{}

Taking in consideration that BOTH classes contains:
- static blocks
- instant blocks
- static variables
- instant variables
- final variables
- no-arg constructors

and inside class Dog, there will be the method main which will initialize a new Dog object

thanks in advance for your help