aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt" Watch "doubt" New topic
Author

doubt

Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44


this code should not compile, right?
Because I think import statement must come after package statement, and cannot be in the middle of the codes. (Correct me if I were wrong)
But the answer given is producing output 8.


Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
Dave Walsh
Greenhorn

Joined: Jun 21, 2007
Posts: 24
Have you tried it ? It doesn't compile: "Imports (if any) must come after package and before class definition".

Who told you the output is 8 ?
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
I was doing John Meyers' Mock Exam. It does not compile when I tried.
I just want to be sure that the reason i gave is correct, isn't it?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi,

if you have such code, it is often the case that you have to put the classes in different files. If you do it right (may the package statements be with you), the code will compile and produce the output 8.
christian combarel
Ranch Hand

Joined: Aug 04, 2007
Posts: 47
Hello,
I didn't try to compile this code but I would say it will fail because x2 is protected and you can't have access to it through a reference but only through inheritance. (I suppose the 2 classes exist in two differents files)



-------------
Chris
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by christian combarel:
I didn't try to compile this code
Then you should do it. It makes the discussion much easier.
sumi selva
Greenhorn

Joined: Jul 10, 2007
Posts: 19
Hi All,
I compiled the below code and didnt get any compile error.It runs fine.
But as per K&B, it should produce a compile error.

Code
------
package A;
public class Father{
static public int x1 = 7;
static protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}

package B;
import A.Father;

public class Son extends Father{

public static void main(String [] arg){
Father f = new Father();
System.out.println( f.x2 );
}
}

May I know why this compiles fine?


Thanks
Sumi
sumi selva
Greenhorn

Joined: Jul 10, 2007
Posts: 19
Hi All,
I got the answer.
If the variable is a static protected one it can be accessed through dor operator in another package.
If it is a protected one without static modifier, it can be accessed only thr inheritance in another package.

Thanks,
Sumi
sumi selva
Greenhorn

Joined: Jul 10, 2007
Posts: 19
Hi All,
One more thing to add..

Given in K&B - Chap 2
"when a subclass-outside-the-package inherits
a protected member, the member is essentially private inside the subclass, such
that only the subclass and its subclasses(both in the same and different package)can access it."

Thanks,
Sumi
Alexsandra Carvalho
Ranch Hand

Joined: Jul 13, 2007
Posts: 75
Hello sumi,

In respect "If the variable is a static protected one it can be accessed through dor operator in another package."

I have tested and really is that. But...it is new for me. Where did you read about this? It was in K&B? What chapter/page?

Thanks,
Alexsandra
 
Don't get me started about those stupid light bulbs.
 
subject: doubt