Alfred Swensen

+ Follow
since May 25, 2009
Alfred likes ...
Netbeans IDE VI Editor PHP
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Alfred Swensen

Oliver Chase wrote:Hi, I do not understand the practical usage of the @Produces annotation.

Sometimes you want to use Dependency Injection like this:

class Foo {
Integer someNumber;

That does, of course, not work because Java has no clue which Number to inject. It could try to call "new Integer()" but that does not work without a constructor parameter.
So you have to create a method that's annotated with @Produces and returns an Integer. Java scans for all @Produce methods and it's presence somewhere on the classpath is enough to let it be used for all Integers that should be injected.

Now the next problem is that you don't want random numbers for *every* variable that happens to be Integer. So you should somehow mark the producer by creating a new inteface with @Qualifier annotation and adding that interface name (@Random in your example) to both, the @Inject an the @Produce. Now Java can match that too and knows that you want the random number here.




I like to migrate an application from Java SE Spring Dependency-Injection using applicationContext-production.xml, applicationContext-devel.xml etc. files to Java EE CDI.

In Spring, I could include several configuration files with the jar and load one at application start (by parsing e.g. a command line parameter --config). So my first question is what would be the best practice to configure a "profile" in JavaEE? Can I somehow switch between web.xml.DEVEL and web.xml.PROD before the CDI dependency injection kicks in?

As I configured not only member objects (@Inject) but also attribute values (@Resource), I would not only have to switch between different beans.xml for different <alternatives> sections but also between different web.xml / ejb-jar.xml for the <env-entry> settings, right? Or should I use some kind of glassfish.xml to configure the settings inside the application container and then retrieve them all with @Resource(name="java:app/env/foo")?



Chinmay Naik wrote:
1) Can an enum constructor throw any checked exceptions?

AFAIK, no. To understand enums I try to think of them as pure syntactic suger, behind the scenes the java compiler could (at least theoretically) be transform them into a plain old java class before compiling them:

If you try to compile that, you see that you get exactly the same error message, but now you understand why the Exception is illegal:

2) Also, can somebody tell me why compiler does not allow me to declare enum constructor of any enum as public or protected. It says, only private is permitted.
However, I could declare enum constructor with no modifier (i.e. with default scope). So why only default scope and private scope is allowed for enum constructors?

public and protected are forbidden because then you would imply that you are planning to call the constructor from outside the class - as Enums may only consist
of compile time constants, this must be prohibited.

Regarding the "default" access level, I tend to use my above explanation that an enum is just a convenience representation and that the compiler will add an "private" implicitly before compiling it.

Except for the claimed error in page 2, which I don't find, they should be.

I've just tried the free mock exam on
As I found it quite cumbersome to figure out which ones are wrong I post my answers below
(I doubt that posting answers without the questions violate any copyright whatsoever),

In page 2 SUN claimes one error. Do you find it?

In page 5 I only get 100% if I answer question 28 with b although it should be
d as the example code does not even compile due to a missing ")" and illegal "7.something();"?!

= Page 1 =

1 be
2 c
3 d
4 ad
5 bce
6 cd

= Page 2 =

7 d
8 b
9 ad
10 a
11 d
12 a

= Page 3 =

13 f
14 d
15 a
16 b
17 c
18 c

= Page 4 =

19 c
20 b
21 de
22 c
23 c
24 ce

= Page 5 =

25 b
26 b
27 a
28 e (Sun says b!!!)
29 e
30 b

= Page 6 =

31 ace
32 c
33 f
34 b
35 b
36 c

= Page 7 =

37 d
38 b
39 c
40 d
41 e
42 d

Pooja Oza wrote:Can some one help me understand the output of the following programs ..... The examples are from SCJP 6 by K&B book, in the self test of multithreading chapter...

The Dudes question:

You run the thread with two newly created DudesChat instances. But that's not the point as the run() method only refers to the d variable which is exactly the same for both threads.
As it is the same, the synchronized keyword in fragment I works as expected, the output will always be "yo yo dude dude" which appears in none of the possible answers.
With fragment two you have a typical unsynchronized case i.e. both methods could theoretically be entered and run at the same time. So F is at least possible.

The exam authors claim that "yo" must always be the first output although I think that it is theoretically possible that one thread sets the flag variable and immediately after that the other thread starts and completes the chat method so that "dude dude" is printed first. Any ideas if this might be possible?

The Chicks question:

You create two threads, each with a *new* instance of the ChicksYack In this new instance the c variable (which is not static as the d in the above example!) was never initialised so if the thread scheduler calls the run() method of these new instances you get a NullPointerException when it tries to call the method .yack(...) on the uninitialized variable c.

The Chess question:

Here, too, you start two threads with different instances of a class. So whether or not you put synchronized before a method is irrelevant as they both only lock their instance.
The output is thus random for both examples. As there are only two threads there can never the case where three different id values are printed so option D is invalid.

Hope that helps!

I cannot find this example in my K&B Java6 from 2008 although there are many similar examples around page 61-64.
Your example cannot work because if the Coffee class is outsite of CoffeeTest3, how can the compile know what CoffeeSize inside Coffee should be?
If Coffee is put inside CoffeeTest3 as the comment suggests, it works.

Hello Zhen

I remember that question from one of the mock exams because I got it wrong the fist time, too.
Truth is, when casting to classes, the compiler checks if the casted variable and the class it should
cast to are in the same Class Hierarchy i.e. if it is theoretically possible that it could work. Trying
to cast "(ArrayList)x" where x is a String would give a compiler error.

But Interfaces are different. My guess is that as classes can implement multiple Interfaces the compiler just gives up trying to be clever here so
you can cast to whatever interface you want - if it does not match at runtime you get the (unchecked) ClassCastException.

As you can assign null to every reference variable it makes sense that you can also cast it to whatever you want.

Yes, it does. Just look it up in the source:

Hi Zhen Zhen

"++" is not a method of the Integer Object y!

If you would have called something like y.increaseByOne() then x and y would both have changed.
But you have done a primitive numeric operation like + - * / ++ -- etc.. using javap you can see from
the disassembled code that the java compiler transformed your code into something like

Integer x = Integer.valueOf(5);
Integer y = Integer.valueOf(6);
x = Integer.valueOf( x.intValue() ++ )

So as with all numeric operators Java tries to make an int value, if it can, from all arguments (here only the x),
does the calculation (here ++) and then assignes it back to the original reference which creates a new
Integer Object on the heap.