aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hard Exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hard Exam question" Watch "Hard Exam question" New topic
Author

Hard Exam question

Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60


Which statements are true to above code snippet.
a) Compilation fails at line 2
b) Compilation fails at line 5
c) compilation fails at line 7
d) compilation fails at line 10
e) compilation OK
[ May 31, 2006: Message edited by: Lucius Stephienn ]

SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Nested types cannot have the same name as an enclosing type.

8.1 Class Declaration
[ May 31, 2006: Message edited by: wise owen ]
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
That's a confusing question because it seems you are using a Contructor (not s nested type).

Enum have their own contructor syntax. But above is not following that syntax. So compilation fails at the first contructor's declaration.
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
Wise Owen was right, compilation fails at line 7 cause of name problem with nested and enclosing type.

Double costructor is fake.
The first MyEnum() {}; is not a counstructor but a Constant (it seems that we can write it with empty brackets).

if the code will look like this (watch out for semicolon in first line):


It would be double constructor.

[ June 01, 2006: Message edited by: Lucius Stephienn ]
[ June 01, 2006: Message edited by: Lucius Stephienn ]
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
i dun understand why it can't have 2 same name constructor?
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
i mean can
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
It can't have and it doesn't.
Look at the following code



It will compile with no problems. Why? As I said earlier, the first "constructor" is actualy a CONSTANT.
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144


I am sorry but I don't get it. Why does this compile? Doesn't enums have their own constructor syntax that constructs and initilaizes at the same time like the following segement from K&B book: (assume there is an int size)

CoffeeSize {BIG(6), LARGE(8), OVERWHELMING (16){}};

I don't have the book with me but that's how I remeber their example was. So for the first constructor (BIG(6)), notice that size will be set to 6.

So the constructor for enums has a very specific format. So why does your new format compiles fine? Thanks
[ June 02, 2006: Message edited by: Firas Zureikat ]
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
Sorry for late answer (I hadn't many free time lately).

This code will compile beacuse actually it's the same as following:


Get it?

And about constructors; their syntax in enums is actually identical as in normal classes. So in line 2, because preceding code has no-arg constructor , we can write like this:

And we can add empty braces (where methods and variables can be defined).

But as long as we invoke no-arg constructor we have to have it (even if we wouldn't define it).

Your won't compile.

CoffeeSize {BIG(6), LARGE(8), OVERWHELMING (16){}};

See why? You dont have a one-arg (with numeric parameter) constructor. So what do you invoke?
[ June 05, 2006: Message edited by: Lucius Stephienn ]
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
Your won't compile.

CoffeeSize {BIG(6), LARGE(8), OVERWHELMING (16){}};

See why? You dont have a one-arg (with numeric parameter) constructor. So what do you invoke?


Sorry for using incomplete code but the following doesn't have a no argument constructor and uses the special constructor calling syntax: (It compiles fine)

enum CoffeeSize {
BIG(8),
HUGE(10),
OVERWHELMING(16) { // start a code block that defines
// the "body" for this constant
public String getLidCode() { // override the method
// defined in CoffeeSize
return "A";
}
}; // <-- the semicolon is REQUIRED when you have a body
CoffeeSize(int ounces) {
this.ounces = ounces;
}
private int ounces;
public int getOunces() {
return ounces;
}
public String getLidCode() { // this method is overridden
// by the OVERWHELMING constant
return "B"; // the default value we want to return for
// CoffeeSize constants
}
}

This code will compile beacuse actually it's the same as following:

code:
--------------------------------------------------------------------------------

public enum MyEnum {
MyEnum;
MyEnum() { };
}

---------------------------------------------------------------------------

Get it?


No I don't. How come yours compiles although you used "MyEnum();"
while mine doesn't when I used "CoffeeSize(1);". My code that doesn't compile is below:



// Only adding "CoffeeSize(1);" makes compilation failure.
// Remove "CoffeeSize(1){};" and it will compile.

enum CoffeeSize {
BIG(8),
HUGE(10),
OVERWHELMING(16) { // start a code block that defines
// the "body" for this constant
public String getLidCode() { // override the method
// defined in CoffeeSize
return "A";
}
}; // <-- the semicolon is REQUIRED when you have a body

CoffeeSize(int ounces) {
this.ounces = ounces;
}

CoffeeSize(1);

private int ounces;
public int getOunces() {
return ounces;
}
public String getLidCode() { // this method is overridden
// by the OVERWHELMING constant
return "B"; // the default value we want to return for
// CoffeeSize constants
}
}
[ June 05, 2006: Message edited by: Firas Zureikat ]
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
Also rememeber that at the bottom of page 64 (K&B Book), the following is mentioned:

"You can NEVER invoke an enum constructor directly. The enum constructor
is invoked automatically, with the arguments you define after the constant
value. For example, BIG(8) invokes the CoffeeSize constructor that takes
an int, passing the int literal 8 to the constructor. (Behind the scenes, of
course, you can imagine that BIG is also passed to the constructor, but we
don't have to know�or care�about the details.)"

But you haven't followed the convention BIG(8) and yet was able to compile by invoking enum constructor directly!! Does this mean the information above in the Kathy and Bert book is incorrect? I might be missing something important.
[ June 05, 2006: Message edited by: Firas Zureikat ]
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
Originally posted by Firas Zureikat:


No I don't. How come yours compiles although you used "MyEnum();"
while mine doesn't when I used "CoffeeSize(1);". My code that doesn't compile is below:

[ June 05, 2006: Message edited by: Firas Zureikat ]


His first MyEnum is a constant. After the comma, it is the constructor.

Your those before the first comma are constants. And your CoffeeSize(1); is an explicit way of invoking the constructor which is not allowed for enums, so you got compilation error.

Basically the morale of this thread is, anything before the "FIRST" comma are treated as constants, after the comma, the method with same name as the enum name without return types, is treated as a constructor. Perfectly legal!

So the code faults on Line 7 because you define enum inside enum, wrong! This is the real culprit. Now you get it?!


SCBCD 1.3: 94%<br />SCWCD 1.4: 91%<br />SCJP 5: 95%
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
Basically the morale of this thread is, anything before the "FIRST" comma are treated as constants, after the comma, the method with same name as the enum name without return types, is treated as a constructor. Perfectly legal!


Ok, thanks you both I got it.

But it is now interesting to notice that multiple semi commas are allowed. I mean the FIRST helps outlines the constant and the second is for the constructor. Is the one for the constructor mandatory?
[ June 06, 2006: Message edited by: Firas Zureikat ]
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
No. the most important is the first comma. x()
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
Sorry, to correct my last comment:
you can define enums within enums, but the name can't be the same.
The compile error: MyEnum is already defined in unnamed package.

If you change the inner MyEnum to another name such as MyEnum2, it compiles.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hard Exam question