File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from K&B Chapter 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from K&B Chapter 5" Watch "Question from K&B Chapter 5" New topic
Author

Question from K&B Chapter 5

a alph
Ranch Hand

Joined: Nov 18, 2008
Posts: 36
Hi,




When I run this program it prints null null.
But I can't understand how. Can you please explain what's happening in lines 1, 2, 3.

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

When I run this program it prints null null.
But I can't understand how. Can you please explain what's happening in lines 1, 2, 3.


Line 1 declares an array of size 2. Line 2 iterates through that array. Hence, the getWeight() method is called twice, at line three.

So, this call getWeight() twice, which prints the "s" variable. And since the s variable is assigned to null, it will print null twice.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
a alph
Ranch Hand

Joined: Nov 18, 2008
Posts: 36
Thanks Henry.

When it iterates through the array, new Mineral() - will this call the constructor of Mineral class?

Thanks.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey A,

The constructor of Mineral class are called while forming the array not while iterating. The the constructor is as called at the line mentioned below -


Regards
Salil Verma
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

can we extend a class without body ???


"Known is a drop , unknown is an ocean " !
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey vamsi ,

Definitely, you can extend a class without body. The example is as mentioned above.. but ideally, if such need arises, blank interface should be preferred than blank class.

You can see many empty (without any function or variable) interfaces in java as well like clonable and serializable.
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

thank you salil ,but what is the use of such classes and interfaces ??? and what is the use of extending them ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9317
    
  17

vamsi acharya wrote:thank you salil ,but what is the use of such classes and interfaces ??? and what is the use of extending them ?


Generally these type of interfaces are called tag or marker interfaces. They maybe used for polymorphism. Read this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Remember that the exam taking environment is very constrained. In general most questions will be stated using no more than 12 lines of code - of course there will be longer bits of code to read thru, but most questions will be stated using 12 lines of code or fewer.

Because of this, the questions often use weird constructs like empty classes, so that the code can focus on the more important issues of the question. In other words, don't expect the code in the questions to use anything like best practices, best practices are a low priority for the folks who create the actual exam questions. Also expect to see horrible stuff like this:



Just remember - the exam creators are trying to jam a lot of code into 12 lines - it gets ugly!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from K&B Chapter 5