Win a copy of Spring Boot in Practice this week in the Spring forum!

Cheung Chau

Greenhorn
+ Follow
since May 25, 2009
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 Cheung Chau

Hi Mark,

Thank you for your reply. Believe it or not I give up searching two sections before that section (10.8) - around section 10.5 and 10.6, where I draw the conclusion that all the solid example will be about database. I also read 'Spring in Action' 3rd ed chapter 6 once - there is no complete example. You actually almost summarized the chapter with your comment

I am looking for and end-to-end tutorial - something that start with POJO class and end with a main class or JUnit test case that execute action in the POJO class or rollback if error. I know that may not exist or require me learning spec like JTA or technology like JBoss AS.

But can you give me some more pointer where to start. Anyway, thank you so much for your time.

-CCC
10 years ago
Hi everyone,

I believe Spring framework support more than just database tranaction. However, it is hard to find an example not including database transaction. Can anyone here give me a pointer on how to find such a tutorial. Thank you

-CCC
10 years ago
Hi Rob,

Thank you for your reply. I wonder if this is the only way? I mean using the Robot class createScreenCapture method with Toolkit.getDefaultToolkit
().getScreenSize(). I am just wondering. Anyway, thank you for your comment or I wouldn't even get to know the Robot class can capture screen shot.

-Cheung
12 years ago
I would like to implement an option in menu that allow me to print a screen shot of my application. Thank you
12 years ago
Hi Henry,

Thank for the explianation. That makes sense. How about why a static protected variable behavior differently from a non-static one. More specifically, why we can use a parent reference or class name to access a static protected variable?

Thank for your time again.

-Cheung
Hi Henry,

Thank you for your reply. I do believe sometime there is no good explaination on certain rule in java. This is possibly one of those cases. However, I was hoping there were some logical explaination behind this. Just like if someone asks me why you can't access non-static variable from static method, I would quote K&B

A static method can't access a nonstatic (instance) variable, because there is no instance! That's not to say there aren't instances of the class alive on the heap, but rather that even if there are, the static method doesn't know anything about them. The same applies to instance methods; a static method can't directly invoke a nonstatic method. Think static = class, nonstatic = instance. Making the method called by the JVM (main()) a static method means the JVM doesn't have to create an instance of your class just to start running code.



I believe Java Spec specify this too but somehow this paragraph engraves this fact into my brain. Anyhow, thank your for your time. I am very interested in reading your java thread book in a near futre ;)

-Cheung
Hi Ninad,

I think you are absolutely correct. However, I am looking for any reasoning behind it. Here is something I came up with after I submitted the original post: all the non-static member variable in a class can be explictly refered to using "this" keyword, which mean current object reference. That mean



refering to the same variable in my example class B or C. Because "this" is representing a instance reference B or C, I think it makes sense that by using obj2 or obj3, we can access pf. I know the logic may be a little broken. For example, It doesn't explain why you can access pf in Class B using obj3, an reference to an instance of Class C.

Also, I am really gald that you brought up

But if protected member is static then it can be accessible to sub class using super class reference.




From a post in this forum, we



You will get:
test.java:10: x4 has protected access in packageA.pack
System.out.println( p.x4 );

We see that we can use p or pack or even test to access x2, but an attempt to access x4 using p failed. As you can see, I understand the rule but don't know why.

Thanks

-Cheung

P.S. Of course, the simplest answer will be JVM or the Java compiler behavior this way
Hi all,

I am pretty sure the following question has been asked before but I couldn't find an answer make sense to me in this forum. The question is from Khalid's SCJP book' review question (ch 4.18)




The answer is lines (1), (3), (4), (5), and (8) will compile. I understand why (1), (5) will compile and (2), (6), (7), (9), (10), (11), (12) WON'T compile because those are clearly explained in the K&B SCJP book. What I find difficult to understand is why, for example in line (3), you can use a subclass reference to access protected member inherited from the superclass. Even stranger is that you can use a reference of C (subclass of B ) to access the variable pf from class A. I know these are facts because I have verified the code using Eclipse. However, I find it difficult to believe because it is stated in K&B book

For a subclass outside the package, the protected member can be accessed only through inheritance.



That's how I rule (2), (6) and (7).

Regards,

-Cheung


Ken,

Thank you for your post. I wonder if you can extend on what you mean by scopes may be independent of each other.

Hi all,


Let's see if we can put a closure to this post. The following is directly from our Java Language Specification

http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html

Here is the part (directly from the language spec), I think (kind of) explain what we have here

If a declaration of an identifier as a local variable of the same method, constructor, or initializer block appears within the scope of a parameter or local variable of the same name, a compile-time error occurs.

Thus the following example does not compile:




This restriction helps to detect some otherwise very obscure bugs. A similar restriction on shadowing of members by local variables was judged impractical, because the addition of a member in a superclass could cause subclasses to have to rename local variables. Related considerations make restrictions on shadowing of local variables by members of nested classes, or on shadowing of local variables by local variables declared within nested classes unattractive as well. Hence, the following example compiles without error:



On the other hand, local variables with the same name may be declared in two separate blocks or for statements neither of which contains the other. Thus:



compiles without error and, when executed, produces the output:

0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1


I said "kind of" because I am still not totally sure about the following statement from the doc

If a declaration of an identifier as a local variable of the same method, constructor, or initializer block appears within the scope of a parameter or local variable of the same name, a compile-time error occurs.



I hope someone can put in plain English. That's why I've always preferred Head First Java or Core Java over the formal Java specification



-Cheung
Hi Jim,

Sorry that I missed all the comment you made below your code block. However, I am still not very clear on what you mean by

I would say it is because it is method level scoped, and when the method is in scope on the stack arguments are passed by value. So even though the method is free to extend beyond itself to the reference static/instance variables it still has the flexibility to re declare those names since they are not in scope

Can you clarify?

Here is my take on this - all local variables are defined on the stack and the stack won't allow code to define variable in the inner block with the same name outside that block. However, unlike static and instance variable, the order matters which mean the inner block won't see the any variable define after the inner block. This make define variable after the block with same name okay.

I don't like the definition above becuase I made it up on the fly based on the review question. I wonder if anyone can offer a more logical and accurate explaination.

Thank you

-Cheung



Hi Jim,

Thank you for taking the time. Again, "WHY" is what I am going for here. I did work out the logic you stated perfectly in your example before my original post - that's why you see the extra example I posted with variable i. What I mean is I would probably answer a similar question correctly in the exam. However, I am interested in knowing the following - let's say we have

1) static scope
2) instance scope
3) local scope
4) block scope

Variable in local scope will hide a variable with the same name in either static or instance scope, but block scope variable CANNOT do the same thing to local scope variable. WHY? Again, I did get the fact but just need a logical explaination.

I couldn't find any detail on this in the book.

Sorry guys, this may not be the right forum to post this question because this is not about getting the exam question but learning Java. However, the question did derived from one of the review question. Thank you for your reply and assisstance.

-Cheung
Hi guys,

Thank you for your reply. Sorry Bob, I will put my code in code block next time (this time).

Hi Jim,

I am totally agree with your explaination on why it failed and that was reason I posted a different example that derived from the review question. However, the real question I would like to get at is whether variable ouch defined in the for loop should hide the method argument and why not?



As you can see, static variable ouch at 1. was hidden by local variable ouch at 2. My question is why CAN'T for-loop variable ouch at 3.(which also is a local variable) hide the local variable ouch at 2. If you read Ankit's reply we see that variable i defined in for-loop ONLY exist in the for-loop and so I believe for-loop does form a scope. If the for-loop forms a scope, why can it hide a variable. That's my original question

Again, I know this is the fact. However, I wonder if there is a logical explaination.


Ankit,

Thank you for your reply. Please see my comment above and see if you can offer more insight. Thank

-Cheung
The answer to the following from chapter 3 review question 8 surprise me

public class Ouch {

static int ouch = 7;
public static void main(String[] args) {
new Ouch().go(ouch);
System.out.print(" " + ouch);

}

void go (int ouch)
{

ouch++;
for(int ouch = 3; ouch < 6; ouch++ );
System.out.print(" " + ouch);
}

}

The answer is E Compilation fails. Since we are in the for loop, should the "go" method argument "ouch" be hidden by for-loop "ouch"? I know the answer is negative but why? Since the following WILL compile

public class Ouch {

static int ouch = 7;
public static void main(String[] args) {
new Ouch().go(ouch);
System.out.print(" " + ouch);

}

void go (int ouch)
{

ouch++;
for(int i = 3; i < 6; i++ );

int i = 2;

System.out.print(" " + i);
}

}

The point is I also defined the variable 'i' twice. However, the following will fail


public class Ouch {

static int ouch = 7;
public static void main(String[] args) {
new Ouch().go(ouch);
System.out.print(" " + ouch);

}

void go (int ouch)
{

ouch++;

int i = 2;

for(int i = 3; i < 6; i++ );



System.out.print(" " + i);
}

}

Note that I define variable i before the for loop.

Thank you for your help

-Cheung

Sorry, this is a dumb question. You got a and b are int from the function signature and return type
Here is the review question (No.4) from chapter 9 of the SCJP java 6 book

***
Assume you have a class that holds two private variables: a and b ...
***

I think the author assume a and b are primitive type (such as int) and therefore in all the answer we have

return a+b;

and the answer states that answer C and D are incorrect are incorrect because only objects can be used to synchronize on, which make sense if you read only the answer. However, I went through all the possible cases. What if variable a and b are Strings? a+b will result in another string. In this case, I would say D is still incorrect because it synchronize on different variable. However, my answer for that question becomes B C and F.

Sorry if people already asked this question. I couldn't find it in the forum using the following key word

thread review question 4

Thanks